2 // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
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.
29 namespace Boo
.Lang
.Compiler
.Steps
32 using Boo
.Lang
.Compiler
.Ast
;
33 using Boo
.Lang
.Compiler
;
34 using Boo
.Lang
.Compiler
.TypeSystem
;
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
);
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
)
87 CompilerErrorFactory
.ClassAlreadyHasBaseType(baseType
,
95 if (baseClass
.IsFinal
&& !TypeSystemServices
.IsError(baseClass
))
98 CompilerErrorFactory
.CannotExtendFinalType(
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
));
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
147 visitedNonInterfaces
= visited
;
148 visitedInterfaces
= new Boo
.Lang
.List();
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
);
170 ResolveBaseTypes(visitedInterfaces
, internalType
.TypeDefinition
);
175 if (visitedNonInterfaces
.Contains(internalType
.TypeDefinition
))
177 Error(CompilerErrorFactory
.InheritanceCycle(baseType
, internalType
.FullName
));
178 node
.BaseTypes
.RemoveAt(index
- removed
);
183 ResolveBaseTypes(visitedNonInterfaces
, internalType
.TypeDefinition
);
190 // Leave special namespace if we entered it before
191 if (type
.GenericInfo
!= null)
199 /// Provides a quasi-namespace that can resolve a type's generic parameters before its base types are bound.
201 internal class GenericParametersNamespaceExtender
: INamespace
206 public GenericParametersNamespaceExtender(IType type
, INamespace currentNamespace
)
209 _parent
= currentNamespace
;
212 public INamespace ParentNamespace
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; }
);
230 targetList
.AddUnique(match
);
237 public IEntity
[] GetMembers()
239 if (_type
.GenericInfo
!= null)
241 return _type
.GenericInfo
.GenericParameters
;
243 return NullNamespace
.EmptyEntityArray
;