[11700] | 1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
| 2 | // |
---|
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
| 4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
| 5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
| 6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
| 7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
| 8 | // |
---|
| 9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
| 10 | // substantial portions of the Software. |
---|
| 11 | // |
---|
| 12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
| 13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
| 14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
| 15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
| 16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 17 | // DEALINGS IN THE SOFTWARE. |
---|
| 18 | |
---|
| 19 | using System; |
---|
| 20 | using System.Collections.Generic; |
---|
| 21 | |
---|
| 22 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
| 23 | { |
---|
| 24 | /// <summary> |
---|
| 25 | /// Helper class for the GetAllBaseTypes() implementation. |
---|
| 26 | /// </summary> |
---|
| 27 | sealed class BaseTypeCollector : List<IType> |
---|
| 28 | { |
---|
| 29 | readonly Stack<IType> activeTypes = new Stack<IType>(); |
---|
| 30 | |
---|
| 31 | /// <summary> |
---|
| 32 | /// If this option is enabled, the list will not contain interfaces when retrieving the base types |
---|
| 33 | /// of a class. |
---|
| 34 | /// </summary> |
---|
| 35 | internal bool SkipImplementedInterfaces; |
---|
| 36 | |
---|
| 37 | public void CollectBaseTypes(IType type) |
---|
| 38 | { |
---|
| 39 | IType def = type.GetDefinition() ?? type; |
---|
| 40 | |
---|
| 41 | // Maintain a stack of currently active type definitions, and avoid having one definition |
---|
| 42 | // multiple times on that stack. |
---|
| 43 | // This is necessary to ensure the output is finite in the presence of cyclic inheritance: |
---|
| 44 | // class C<X> : C<C<X>> {} would not be caught by the 'no duplicate output' check, yet would |
---|
| 45 | // produce infinite output. |
---|
| 46 | if (activeTypes.Contains(def)) |
---|
| 47 | return; |
---|
| 48 | activeTypes.Push(def); |
---|
| 49 | // Note that we also need to push non-type definitions, e.g. for protecting against |
---|
| 50 | // cyclic inheritance in type parameters (where T : S where S : T). |
---|
| 51 | // The output check doesn't help there because we call Add(type) only at the end. |
---|
| 52 | // We can't simply call this.Add(type); at the start because that would return in an incorrect order. |
---|
| 53 | |
---|
| 54 | // Avoid outputting a type more than once - necessary for "diamond" multiple inheritance |
---|
| 55 | // (e.g. C implements I1 and I2, and both interfaces derive from Object) |
---|
| 56 | if (!this.Contains(type)) { |
---|
| 57 | foreach (IType baseType in type.DirectBaseTypes) { |
---|
| 58 | if (SkipImplementedInterfaces && def != null && def.Kind != TypeKind.Interface && def.Kind != TypeKind.TypeParameter) { |
---|
| 59 | if (baseType.Kind == TypeKind.Interface) { |
---|
| 60 | // skip the interface |
---|
| 61 | continue; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | CollectBaseTypes(baseType); |
---|
| 65 | } |
---|
| 66 | // Add(type) at the end - we want a type to be output only after all its base types were added. |
---|
| 67 | this.Add(type); |
---|
| 68 | // Note that this is not the same as putting the this.Add() call in front and then reversing the list. |
---|
| 69 | // For the diamond inheritance, Add() at the start produces "C, I1, Object, I2", |
---|
| 70 | // while Add() at the end produces "Object, I1, I2, C". |
---|
| 71 | } |
---|
| 72 | activeTypes.Pop(); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|