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.
31 using Boo
.Lang
.Compiler
;
32 using Boo
.Lang
.Compiler
.Ast
;
33 using Boo
.Lang
.Compiler
.TypeSystem
;
35 namespace Boo
.Lang
.Compiler
.Steps
37 public class ForeignReferenceCollector
: DepthFirstVisitor
, IDisposable
, ICompilerComponent
43 Method _currentMethod
;
47 Hash _referencedEntities
;
49 SelfEntity _selfEntity
;
51 CompilerContext _context
;
53 public ForeignReferenceCollector()
55 _references
= new List();
56 _referencedEntities
= new Hash();
59 public Node SourceNode
72 public Method CurrentMethod
76 return _currentMethod
;
81 _currentMethod
= value;
85 public IType CurrentType
95 if (null != _selfEntity
)
97 _selfEntity
.Type
= value;
102 public List References
110 public Hash ReferencedEntities
114 return _referencedEntities
;
118 public bool ContainsForeignLocalReferences
122 foreach (IEntity entity
in _referencedEntities
.Keys
)
124 EntityType type
= entity
.EntityType
;
125 if (EntityType
.Local
== type
||
126 EntityType
.Parameter
== type
)
135 protected IEntity
GetSelfEntity()
137 if (null == _selfEntity
)
139 _selfEntity
= new SelfEntity("this", CurrentType
);
144 protected BooCodeBuilder CodeBuilder
148 return _context
.CodeBuilder
;
152 public void Initialize(CompilerContext context
)
154 if (null == _currentType
)
156 throw new InvalidOperationException("CurrentType was not properly initialized!");
161 public void Dispose()
164 _currentMethod
= null;
167 _referencedEntities
.Clear();
170 public BooClassBuilder
CreateSkeletonClass(string name
, LexicalInfo lexicalInfo
)
172 BooClassBuilder builder
= CodeBuilder
.CreateClass(name
);
173 builder
.Modifiers
|= TypeMemberModifiers
.Internal
;
174 builder
.LexicalInfo
= lexicalInfo
;
176 builder
.AddBaseType(CodeBuilder
.TypeSystemServices
.ObjectType
);
177 DeclareFieldsAndConstructor(builder
);
181 public void DeclareFieldsAndConstructor(BooClassBuilder builder
)
183 // referenced entities turn into fields
184 foreach (ITypedEntity entity
in Builtins
.array(_referencedEntities
.Keys
))
186 Field field
= builder
.AddInternalField("__" + entity
.Name
+ _context
.AllocIndex(), entity
.Type
);
187 _referencedEntities
[entity
] = field
.Entity
;
190 // single constructor taking all referenced entities
191 BooMethodBuilder constructor
= builder
.AddConstructor();
192 constructor
.Modifiers
= TypeMemberModifiers
.Public
;
193 constructor
.Body
.Add(CodeBuilder
.CreateSuperConstructorInvocation(builder
.Entity
.BaseType
));
194 foreach (ITypedEntity entity
in _referencedEntities
.Keys
)
196 InternalField field
= (InternalField
)_referencedEntities
[entity
];
197 ParameterDeclaration parameter
= constructor
.AddParameter(field
.Name
, entity
.Type
);
198 constructor
.Body
.Add(
199 CodeBuilder
.CreateAssignment(CodeBuilder
.CreateReference(field
),
200 CodeBuilder
.CreateReference(parameter
)));
204 public void AdjustReferences()
206 foreach (Expression reference
in _references
)
208 InternalField entity
= (InternalField
)_referencedEntities
[reference
.Entity
];
211 reference
.ParentNode
.Replace(reference
,
212 CodeBuilder
.CreateReference(entity
));
217 public MethodInvocationExpression
CreateConstructorInvocationWithReferencedEntities(IType type
)
219 MethodInvocationExpression mie
= CodeBuilder
.CreateConstructorInvocation(type
.GetConstructors()[0]);
220 foreach (ITypedEntity entity
in _referencedEntities
.Keys
)
222 mie
.Arguments
.Add(CreateForeignReference(entity
));
227 public Expression
CreateForeignReference(IEntity entity
)
229 if (_selfEntity
== entity
)
231 return CodeBuilder
.CreateSelfReference(CurrentType
);
235 return CodeBuilder
.CreateReference(entity
);
239 override public void OnReferenceExpression(ReferenceExpression node
)
241 if (IsForeignReference(node
))
243 _references
.Add(node
);
244 _referencedEntities
[node
.Entity
] = null;
248 override public void OnSelfLiteralExpression(SelfLiteralExpression node
)
250 IEntity entity
= GetSelfEntity();
251 node
.Entity
= entity
;
252 _references
.Add(node
);
253 _referencedEntities
[entity
] = null;
256 bool IsForeignReference(ReferenceExpression node
)
258 IEntity entity
= node
.Entity
;
261 EntityType type
= entity
.EntityType
;
262 if (type
== EntityType
.Local
)
264 return null == _currentMethod
||
265 !_currentMethod
.Locals
.ContainsEntity(entity
);
267 else if (type
== EntityType
.Parameter
)
269 return null == _currentMethod
||
270 !_currentMethod
.Parameters
.ContainsEntity(entity
);