2 // ast.cs: Base class for the EcmaScript program tree representation.
5 // Cesar Lopez Nataren (cesar@ciencias.unam.mx)
7 // (C) 2003, Cesar Lopez Nataren
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
;
33 using System
.Reflection
;
34 using System
.Reflection
.Emit
;
36 namespace Microsoft
.JScript
{
38 public abstract class AST
{
41 protected int line_number
;
44 // Here the actual IL code generation happens.
46 internal abstract void Emit (EmitContext ec
);
49 // Perform type checks and associates expressions
50 // with their declarations
52 internal abstract bool Resolve (IdentificationTable context
);
56 if (parent
== null || parent
is ScriptBlock
)
58 else if (parent
is DoWhile
|| parent
is While
|| parent
is For
|| parent
is ForIn
)
65 private bool InSwitch
{
69 else if (parent
is Switch
)
72 return parent
.InSwitch
;
76 private bool InFunction
{
80 else if (parent
is FunctionDeclaration
|| parent
is FunctionExpression
)
83 return parent
.InFunction
;
87 private Function GetContainerFunction
{
91 if (parent
is Function
)
92 return parent
as Function
;
93 return parent
.GetContainerFunction
;
99 public abstract class Function
: AST
102 bool ignore_dynamic_scope
;
103 bool requires_activation
;
105 internal string prefix
;
106 internal FunctionObject func_obj
;
107 internal JSFunctionAttributeEnum func_type
;
109 internal DictionaryEntry
[] locals
;
110 internal LocalBuilder local_func
;
112 protected bool not_void_return
= false;
114 internal bool CheckThis
{
115 get { return check_this; }
116 set { check_this = value; }
119 internal bool IgnoreDynamicScope
{
120 get { return ignore_dynamic_scope; }
121 set { ignore_dynamic_scope = value; }
124 internal bool RequiresActivation
{
125 get { return requires_activation; }
126 set { requires_activation = value; }
129 public void Init (Block body
, FormalParameterList p
)
131 func_obj
.body
= body
;
132 func_obj
.parameters
= p
;
135 internal void set_prefix ()
137 if (parent
!= null && parent
is Function
) {
139 tmp
= (Function
) parent
;
140 if (tmp
.prefix
!= String
.Empty
)
141 prefix
= tmp
.prefix
+ "." + tmp
.func_obj
.name
;
143 prefix
= tmp
.func_obj
.name
;
145 prefix
= String
.Empty
;
148 internal void set_function_type ()
150 if (parent
== null || parent
.GetType () == typeof (ScriptBlock
))
151 func_type
= JSFunctionAttributeEnum
.ClassicFunction
;
152 else if (parent
is FunctionDeclaration
)
153 func_type
= JSFunctionAttributeEnum
.NestedFunction
;
156 internal void set_custom_attr (MethodBuilder mb
)
158 CustomAttributeBuilder attr_builder
;
159 Type func_attr
= typeof (JSFunctionAttribute
);
160 Type
[] func_attr_enum
= new Type
[] {typeof (JSFunctionAttributeEnum)}
;
161 attr_builder
= new CustomAttributeBuilder (func_attr
.GetConstructor (func_attr_enum
),
162 new object [] {func_type}
);
163 mb
.SetCustomAttribute (attr_builder
);
166 internal void NotVoidReturnHappened (object sender
, NotVoidReturnEventArgs args
)
168 not_void_return
= true;
171 protected Type HandleReturnType
{
175 ret_type
= typeof (object);
177 ret_type
= typeof (void);
182 internal void emit_return_local_field (ILGenerator ig
, ConstructorInfo ctr_info
, int n
)
184 ig
.Emit (OpCodes
.Dup
);
185 ig
.Emit (OpCodes
.Ldc_I4
, n
);
186 ig
.Emit (OpCodes
.Ldstr
, "return value");
187 ig
.Emit (OpCodes
.Ldtoken
, typeof (object));
188 ig
.Emit (OpCodes
.Ldc_I4
, n
);
189 ig
.Emit (OpCodes
.Newobj
, ctr_info
);
190 ig
.Emit (OpCodes
.Stelem_Ref
);