5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using ICSharpCode.NRefactory.TypeSystem;
28
using System.Collections.Generic;
31
namespace ICSharpCode.NRefactory.CSharp
34
/// Used to check the compatibility state of two compilations.
36
public enum AbiCompatibility
44
/// Some items got added, but the ABI remains to be compatible
49
/// The ABI has changed
55
public sealed class AbiEventArgs : EventArgs
57
public string Message { get; set; }
59
public AbiEventArgs(string message)
61
this.Message = message;
66
/// The Abi comparer checks the public API of two compilation and determines the compatibility state.
68
public class AbiComparer
70
public bool StopOnIncompatibility {
74
void CheckContstraints(IType otype, ITypeParameter p1, ITypeParameter p2, ref AbiCompatibility compatibility)
76
if (p1.DirectBaseTypes.Count () != p2.DirectBaseTypes.Count () ||
77
p1.HasReferenceTypeConstraint != p2.HasReferenceTypeConstraint ||
78
p1.HasValueTypeConstraint != p2.HasValueTypeConstraint ||
79
p1.HasDefaultConstructorConstraint != p2.HasDefaultConstructorConstraint) {
80
OnIncompatibilityFound (new AbiEventArgs (string.Format (TranslateString ("Type parameter constraints of type {0} have changed."), otype.FullName)));
81
compatibility = AbiCompatibility.Incompatible;
85
void CheckContstraints(IMethod omethod, ITypeParameter p1, ITypeParameter p2, ref AbiCompatibility compatibility)
87
if (p1.DirectBaseTypes.Count () != p2.DirectBaseTypes.Count () ||
88
p1.HasReferenceTypeConstraint != p2.HasReferenceTypeConstraint ||
89
p1.HasValueTypeConstraint != p2.HasValueTypeConstraint ||
90
p1.HasDefaultConstructorConstraint != p2.HasDefaultConstructorConstraint) {
91
OnIncompatibilityFound (new AbiEventArgs (string.Format (TranslateString ("Type parameter constraints of method {0} have changed."), omethod.FullName)));
92
compatibility = AbiCompatibility.Incompatible;
96
void CheckTypes (ITypeDefinition oType, ITypeDefinition nType, ref AbiCompatibility compatibility)
98
int oldMemberCount = 0;
99
Predicate<IUnresolvedMember> pred = null;
100
if (oType.Kind == TypeKind.Class || oType.Kind == TypeKind.Struct)
101
pred = m => (m.IsPublic || m.IsProtected) && !m.IsOverride && !m.IsSynthetic;
103
for (int i = 0; i < oType.TypeParameterCount; i++) {
104
CheckContstraints (oType, oType.TypeParameters[i], nType.TypeParameters[i], ref compatibility);
105
if (compatibility == AbiCompatibility.Incompatible && StopOnIncompatibility)
109
foreach (var member in oType.GetMembers (pred, GetMemberOptions.IgnoreInheritedMembers)) {
110
var newMember = nType.GetMembers (m => member.UnresolvedMember.Name == m.Name && m.IsPublic == member.IsPublic && m.IsProtected == member.IsProtected);
111
var equalMember = newMember.FirstOrDefault (m => SignatureComparer.Ordinal.Equals (member, m));
112
if (equalMember == null) {
113
compatibility = AbiCompatibility.Incompatible;
114
if (StopOnIncompatibility)
118
var om = member as IMethod;
120
for (int i = 0; i < om.TypeParameters.Count; i++) {
121
CheckContstraints (om, om.TypeParameters[i], ((IMethod)equalMember).TypeParameters[i], ref compatibility);
122
if (compatibility == AbiCompatibility.Incompatible && StopOnIncompatibility)
129
if (compatibility == AbiCompatibility.Bigger && oType.Kind != TypeKind.Interface)
131
if (oldMemberCount != nType.GetMembers (pred, GetMemberOptions.IgnoreInheritedMembers).Count ()) {
132
if (oType.Kind == TypeKind.Interface) {
133
OnIncompatibilityFound (new AbiEventArgs (string.Format (TranslateString ("Interafce {0} has changed."), oType.FullName)));
134
compatibility = AbiCompatibility.Incompatible;
136
if (compatibility == AbiCompatibility.Equal)
137
compatibility = AbiCompatibility.Bigger;
142
void CheckNamespace(INamespace oNs, INamespace nNs, ref AbiCompatibility compatibility)
144
foreach (var type in oNs.Types) {
145
if (!type.IsPublic && !type.IsProtected)
147
var newType = nNs.GetTypeDefinition (type.Name, type.TypeParameterCount);
148
if (newType == null) {
149
OnIncompatibilityFound (new AbiEventArgs (string.Format (TranslateString ("Type definition {0} is missing."), type.FullName)));
150
compatibility = AbiCompatibility.Incompatible;
151
if (StopOnIncompatibility)
155
CheckTypes (type, newType, ref compatibility);
156
if (compatibility == AbiCompatibility.Incompatible && StopOnIncompatibility)
160
if (compatibility == AbiCompatibility.Bigger)
162
foreach (var type in nNs.Types) {
163
if (!type.IsPublic && !type.IsProtected)
165
if (oNs.GetTypeDefinition (type.Name, type.TypeParameterCount) == null) {
166
if (compatibility == AbiCompatibility.Equal)
167
compatibility = AbiCompatibility.Bigger;
173
static bool ContainsPublicTypes(INamespace testNs)
175
var stack = new Stack<INamespace> ();
177
while (stack.Count > 0) {
178
var ns = stack.Pop ();
179
if (ns.Types.Any (t => t.IsPublic))
181
foreach (var child in ns.ChildNamespaces)
188
/// Check the specified oldProject and newProject if they're compatible.
190
/// <param name="oldProject">Old project.</param>
191
/// <param name="newProject">New project.</param>
192
public AbiCompatibility Check (ICompilation oldProject, ICompilation newProject)
194
var oldStack = new Stack<INamespace> ();
195
var newStack = new Stack<INamespace> ();
196
oldStack.Push (oldProject.MainAssembly.RootNamespace);
197
newStack.Push (newProject.MainAssembly.RootNamespace);
199
AbiCompatibility compatibility = AbiCompatibility.Equal;
200
while (oldStack.Count > 0) {
201
var oNs = oldStack.Pop ();
202
var nNs = newStack.Pop ();
204
CheckNamespace (oNs, nNs, ref compatibility);
205
if (compatibility == AbiCompatibility.Incompatible && StopOnIncompatibility)
206
return AbiCompatibility.Incompatible;
207
foreach (var child in oNs.ChildNamespaces) {
208
var newChild = nNs.GetChildNamespace (child.Name);
209
if (newChild == null) {
210
OnIncompatibilityFound (new AbiEventArgs (string.Format (TranslateString ("Namespace {0} is missing."), child.FullName)));
211
if (StopOnIncompatibility)
212
return AbiCompatibility.Incompatible;
215
oldStack.Push (child);
216
newStack.Push (newChild);
219
// check if namespaces are added
220
if (compatibility != AbiCompatibility.Bigger) {
221
foreach (var child in nNs.ChildNamespaces) {
222
if (oNs.GetChildNamespace (child.Name) == null) {
223
if (compatibility == AbiCompatibility.Equal && ContainsPublicTypes (child))
224
compatibility = AbiCompatibility.Bigger;
230
return compatibility;
233
public virtual string TranslateString(string str)
238
public event EventHandler<AbiEventArgs> IncompatibilityFound;
240
protected virtual void OnIncompatibilityFound(AbiEventArgs e)
242
var handler = IncompatibilityFound;