2005-11-24 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / FormalParameterList.cs
1 //
2 // FormalParameterList.cs: A list of identifiers.
3 //
4 // Author:
5 //      Cesar Lopez Nataren
6 //
7 // (C) 2003, Cesar Lopez Nataren, <cesar@ciencias.unam.mx>
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Reflection.Emit;
32 using System.Collections;
33 using System.Text;
34 using System;
35
36 namespace Microsoft.JScript {
37
38         internal class FormalParam : AST {
39                 internal string id;
40                 internal string type_annot;
41                 internal byte pos;
42
43                 //
44                 // FIXME: 
45                 //      Must perform semantic analysis on type_annot,
46                 //      and assign that type value to 'type' if valid.
47                 //
48                 internal Type type = typeof (Object);
49
50                 internal FormalParam (string id, string type_annot, Location location)
51                         : base (null, location)
52                 {
53                         this.id = id;
54                         this.type_annot = type_annot;
55                 }
56
57                 public override string ToString ()
58                 {
59                         return id + " " + type_annot;
60                 }
61
62                 internal override void Emit (EmitContext ec)
63                 {
64                 }
65
66                 internal override bool Resolve (Environment env)
67                 {
68                         env.Enter (String.Empty, Symbol.CreateSymbol (id), this);
69                         return true;
70                 }
71         }
72                         
73         internal class FormalParameterList : AST {
74
75                 internal ArrayList ids;
76
77                 internal FormalParameterList (Location location)
78                         : base (null, location)
79                 {
80                         ids = new ArrayList ();
81                 }
82
83                 internal void Add (string id, string type_annot, Location location)
84                 {
85                         FormalParam p = new FormalParam (id, type_annot, location);
86                         ids.Add (p);
87                 }
88
89                 public override string ToString ()
90                 {
91                         StringBuilder sb = new StringBuilder ();
92                 
93                         foreach (FormalParam f in ids)
94                                 sb.Append (f.ToString () + " ");
95                 
96                         return sb.ToString ();
97                 }
98
99                 internal override bool Resolve (Environment env)
100                 {
101                         FormalParam f;
102                         int i, n = ids.Count;
103
104                         for (i = 0; i < n; i++) {
105                                 f = (FormalParam) ids [i];
106                                 f.pos = (byte) (i + 2);
107                                 f.Resolve (env);
108                         }
109                         return true;
110                 } 
111
112                 internal override void Emit (EmitContext ec)
113                 {
114                         int n = ids.Count;
115                         ILGenerator ig = ec.ig;
116
117                         ig.Emit (OpCodes.Ldc_I4, n);
118                         ig.Emit (OpCodes.Newarr, typeof (string));
119
120                         for (int i = 0; i < n; i++) {
121                                 ig.Emit (OpCodes.Dup);
122                                 ig.Emit (OpCodes.Ldc_I4, i);
123                                 ig.Emit (OpCodes.Ldstr, ((FormalParam) ids [i]).id);
124                                 ig.Emit (OpCodes.Stelem_Ref);
125                         }
126                 }
127
128                 internal int size {
129                         get { return ids.Count; }
130                 }
131
132                 internal FormalParam get_element (int i)
133                 {
134                         if (i >= 0 && i < size)
135                                 return (FormalParam) ids [i];
136                         else
137                                 throw new IndexOutOfRangeException ();
138                 }
139         }
140 }