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
.TypeSystem
32 using Boo
.Lang
.Compiler
.Ast
;
34 public class InternalMethod
: IInternalEntity
, IMethod
, INamespace
36 protected TypeSystemServices _typeSystemServices
;
38 protected Method _method
;
40 protected IMethod _override
;
42 protected ICallableType _type
;
44 protected IType _declaringType
;
46 protected IParameter
[] _parameters
;
48 protected ExpressionCollection _returnExpressions
;
50 protected List _yieldStatements
;
52 private bool? _isBooExtension
;
53 private bool? _isClrExtension
;
55 internal InternalMethod(TypeSystemServices typeSystemServices
, Method method
)
57 _typeSystemServices
= typeSystemServices
;
59 if (method
.NodeType
!= NodeType
.Constructor
&& method
.NodeType
!= NodeType
.Destructor
)
61 if (null == _method
.ReturnType
)
63 IType returnType
= _method
.DeclaringType
.NodeType
== NodeType
.ClassDefinition
65 : (IType
)_typeSystemServices
.VoidType
;
66 _method
.ReturnType
= _typeSystemServices
.CodeBuilder
.CreateTypeReference(method
.LexicalInfo
, returnType
);
71 public bool IsExtension
75 return IsBooExtension
|| IsClrExtension
;
79 public bool IsBooExtension
83 if (null == _isBooExtension
)
85 _isBooExtension
= IsAttributeDefined(Types
.BooExtensionAttribute
);
87 return _isBooExtension
.Value
;
91 public bool IsClrExtension
95 if (null == _isClrExtension
)
97 _isClrExtension
= MetadataUtil
.HasClrExtensions()
98 && IsAttributeDefined(Types
.ClrExtensionAttribute
);
100 return _isClrExtension
.Value
;
104 public bool IsDuckTyped
108 return this.ReturnType
== _typeSystemServices
.DuckType
;
112 public bool IsPInvoke
116 return IsAttributeDefined(Types
.DllImportAttribute
);
120 private bool IsAttributeDefined(System
.Type attributeType
)
122 return MetadataUtil
.IsAttributeDefined(_method
, _typeSystemServices
.Map(attributeType
));
125 public IType DeclaringType
129 if (null == _declaringType
)
131 _declaringType
= (IType
)TypeSystemServices
.GetEntity(_method
.DeclaringType
);
133 return _declaringType
;
141 return _method
.IsStatic
;
149 return _method
.IsPublic
;
153 public bool IsProtected
157 return _method
.IsProtected
;
161 public bool IsPrivate
165 return _method
.IsPrivate
;
169 public bool IsInternal
173 return _method
.IsInternal
;
177 public bool IsAbstract
181 return _method
.IsAbstract
;
185 public bool IsVirtual
189 return _method
.IsVirtual
190 || _method
.IsAbstract
191 || _method
.IsOverride
;
195 public bool IsSpecialName
211 public bool AcceptVarArgs
215 return _method
.Parameters
.VariableNumber
;
219 public virtual string FullName
223 return _method
.DeclaringType
.FullName
+ "." + _method
.Name
;
227 public virtual EntityType EntityType
231 return EntityType
.Method
;
235 public ICallableType CallableType
241 _type
= _typeSystemServices
.GetCallableType(this);
271 public IMethod Overriden
284 public IParameter
[] GetParameters()
286 if (null == _parameters
)
288 _parameters
= _typeSystemServices
.Map(_method
.Parameters
);
293 public virtual IType ReturnType
297 return TypeSystemServices
.GetType(_method
.ReturnType
);
301 public INamespace ParentNamespace
305 return DeclaringType
;
309 public bool IsGenerator
313 return null != _yieldStatements
;
317 public ExpressionCollection ReturnExpressions
321 return _returnExpressions
;
325 public ExpressionCollection YieldExpressions
329 ExpressionCollection expressions
= new ExpressionCollection();
330 foreach (YieldStatement stmt
in _yieldStatements
)
332 if (null != stmt
.Expression
) expressions
.Add(stmt
.Expression
);
339 class LabelCollector
: DepthFirstVisitor
341 public static readonly InternalLabel
[] EmptyInternalLabelArray
= new InternalLabel
[0];
343 protected List _labels
;
345 public override void OnLabelStatement(LabelStatement node
)
347 if (null == _labels
) _labels
= new List();
348 _labels
.Add(node
.Entity
);
351 public InternalLabel
[] Labels
355 if (null == _labels
) return EmptyInternalLabelArray
;
356 return (InternalLabel
[])_labels
.ToArray(new InternalLabel
[_labels
.Count
]);
361 public InternalLabel
[] Labels
365 LabelCollector collector
= new LabelCollector();
366 _method
.Accept(collector
);
367 return collector
.Labels
;
371 public void AddYieldStatement(YieldStatement stmt
)
373 if (null == _yieldStatements
) _yieldStatements
= new List();
374 _yieldStatements
.Add(stmt
);
377 public void AddReturnExpression(Expression expression
)
379 if (null == _returnExpressions
) _returnExpressions
= new ExpressionCollection();
380 _returnExpressions
.Add(expression
);
383 public Local
ResolveLocal(string name
)
385 foreach (Local local
in _method
.Locals
)
387 if (local
.PrivateScope
) continue;
388 if (name
== local
.Name
) return local
;
393 public ParameterDeclaration
ResolveParameter(string name
)
395 foreach (ParameterDeclaration parameter
in _method
.Parameters
)
397 if (name
== parameter
.Name
) return parameter
;
402 public virtual bool Resolve(List targetList
, string name
, EntityType flags
)
404 if (NameResolutionService
.IsFlagSet(flags
, EntityType
.Local
))
406 Local local
= ResolveLocal(name
);
409 targetList
.Add(TypeSystemServices
.GetEntity(local
));
414 if (NameResolutionService
.IsFlagSet(flags
, EntityType
.Parameter
))
416 ParameterDeclaration parameter
= ResolveParameter(name
);
417 if (null != parameter
)
419 targetList
.Add(TypeSystemServices
.GetEntity(parameter
));
427 IEntity
[] INamespace
.GetMembers()
429 return NullNamespace
.EmptyEntityArray
;
432 override public string ToString()
434 return _typeSystemServices
.GetSignature(this);
437 public virtual IConstructedMethodInfo ConstructedInfo
442 public virtual IGenericMethodInfo GenericInfo