BOO-988: Added else block to the for and while statements, and include a suite of...
[boo.git] / src / Boo.Lang.Compiler / Steps / BindBaseTypes.cs
blobbcdb996a9b2b45ab30baad740c33b10ff53ced78
1 #region license
2 // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Rodrigo B. de Oliveira nor the names of its
14 // contributors may be used to endorse or promote products derived from this
15 // software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #endregion
29 namespace Boo.Lang.Compiler.Steps
31 using System;
32 using Boo.Lang.Compiler.Ast;
33 using Boo.Lang.Compiler;
34 using Boo.Lang.Compiler.TypeSystem;
36 [Serializable]
37 public class BindBaseTypes : AbstractNamespaceSensitiveVisitorCompilerStep
39 public BindBaseTypes()
43 override public void Run()
45 Visit(CompileUnit.Modules);
48 override public void OnEnumDefinition(EnumDefinition node)
52 override public void OnClassDefinition(ClassDefinition node)
54 // Visit type definition's members to resolve base types on nested types
55 base.OnClassDefinition(node);
57 // Resolve and check base types
58 ResolveBaseTypes(new Boo.Lang.List(), node);
59 CheckBaseTypes(node);
61 if (!node.IsFinal)
63 if (((IType)node.Entity).IsFinal)
65 node.Modifiers |= TypeMemberModifiers.Final;
70 override public void OnInterfaceDefinition(InterfaceDefinition node)
72 ResolveBaseTypes(new Boo.Lang.List(), node);
73 CheckInterfaceBaseTypes(node);
76 void CheckBaseTypes(ClassDefinition node)
78 IType baseClass = null;
79 foreach (TypeReference baseType in node.BaseTypes)
81 IType baseInfo = GetType(baseType);
82 if (!baseInfo.IsInterface)
84 if (null != baseClass)
86 Error(
87 CompilerErrorFactory.ClassAlreadyHasBaseType(baseType,
88 node.Name,
89 baseClass.FullName)
92 else
94 baseClass = baseInfo;
95 if (baseClass.IsFinal && !TypeSystemServices.IsError(baseClass))
97 Error(
98 CompilerErrorFactory.CannotExtendFinalType(
99 baseType,
100 baseClass.FullName));
106 if (null == baseClass)
108 node.BaseTypes.Insert(0, CodeBuilder.CreateTypeReference(TypeSystemServices.ObjectType) );
112 void CheckInterfaceBaseTypes(InterfaceDefinition node)
114 foreach (TypeReference baseType in node.BaseTypes)
116 IType tag = GetType(baseType);
117 if (!tag.IsInterface)
119 Error(CompilerErrorFactory.InterfaceCanOnlyInheritFromInterface(baseType, node.FullName, tag.FullName));
124 void ResolveBaseTypes(Boo.Lang.List visited, TypeDefinition node)
126 // If type is generic, enter a special namespace to allow
127 // correct resolution of generic parameters
128 IType type = (IType)TypeSystemServices.GetEntity(node);
129 if (type.GenericInfo != null)
131 EnterNamespace(new GenericParametersNamespaceExtender(
132 type, NameResolutionService.CurrentNamespace));
135 visited.Add(node);
137 Boo.Lang.List visitedNonInterfaces = null;
138 Boo.Lang.List visitedInterfaces = null;
140 if (node is InterfaceDefinition)
142 visitedInterfaces = visited;
143 // interfaces won't have noninterface base types so visitedNonInterfaces not necessary here
145 else
147 visitedNonInterfaces = visited;
148 visitedInterfaces = new Boo.Lang.List();
151 int removed = 0;
152 int index = 0;
153 foreach (SimpleTypeReference baseType in node.BaseTypes.ToArray())
155 NameResolutionService.ResolveSimpleTypeReference(baseType);
157 AbstractInternalType internalType = baseType.Entity as AbstractInternalType;
158 if (null != internalType)
160 if (internalType is InternalInterface)
162 if (visitedInterfaces.Contains(internalType.TypeDefinition))
164 Error(CompilerErrorFactory.InheritanceCycle(baseType, internalType.FullName));
165 node.BaseTypes.RemoveAt(index - removed);
166 ++removed;
168 else
170 ResolveBaseTypes(visitedInterfaces, internalType.TypeDefinition);
173 else
175 if (visitedNonInterfaces.Contains(internalType.TypeDefinition))
177 Error(CompilerErrorFactory.InheritanceCycle(baseType, internalType.FullName));
178 node.BaseTypes.RemoveAt(index - removed);
179 ++removed;
181 else
183 ResolveBaseTypes(visitedNonInterfaces, internalType.TypeDefinition);
187 ++index;
190 // Leave special namespace if we entered it before
191 if (type.GenericInfo != null)
193 LeaveNamespace();
198 /// <summary>
199 /// Provides a quasi-namespace that can resolve a type's generic parameters before its base types are bound.
200 /// </summary>
201 internal class GenericParametersNamespaceExtender : INamespace
203 IType _type;
204 INamespace _parent;
206 public GenericParametersNamespaceExtender(IType type, INamespace currentNamespace)
208 _type = type;
209 _parent = currentNamespace;
212 public INamespace ParentNamespace
214 get
216 return _parent;
220 public bool Resolve(List targetList, string name, EntityType filter)
222 if (_type.GenericInfo != null && filter == EntityType.Type)
224 IGenericParameter match = Array.Find(
225 _type.GenericInfo.GenericParameters,
226 delegate(IGenericParameter gp) { return gp.Name == name; });
228 if (match != null)
230 targetList.AddUnique(match);
231 return true;
234 return false;
237 public IEntity[] GetMembers()
239 if (_type.GenericInfo != null)
241 return _type.GenericInfo.GenericParameters;
243 return NullNamespace.EmptyEntityArray;