* Block.cs, FunctionObject.cs, VariableDeclaration.cs: Added 'AST parent' field.
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / FunctionObject.cs
1 //
2 // FunctionObject.cs:
3 //
4 // Author:
5 //      Cesar Octavio Lopez Nataren
6 //
7 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
8 //
9
10 using System;
11 using System.Text;
12 using System.Reflection;
13 using System.Collections;
14
15 namespace Microsoft.JScript {
16
17         public class FunctionObject : ScriptFunction {
18
19                 internal MethodAttributes attr;
20                 internal string name;
21                 internal string type_annot;
22                 internal Type return_type;
23                 internal FormalParameterList parameters;
24                 internal Block body;
25                 internal AST parent; 
26
27                 internal FunctionObject (AST parent, string name,
28                                          FormalParameterList p,
29                                          string ret_type, Block body)
30                 {
31                         //
32                         // FIXME: 
33                         // 1) Must collect the attributes given.
34                         // 2) Check if they are semantically correct.
35                         // 3) Assign those values to 'attr'.
36                         //
37                         this.attr = MethodAttributes.Public | MethodAttributes.Static;
38
39                         this.parent = parent;
40                         this.name = name;
41                         this.parameters = p;
42
43                         this.type_annot = ret_type;
44                         //
45                         // FIXME: Must check that return_type it's a valid type,
46                         // and assign that to 'return_type' field.
47                         //
48                         this.return_type = typeof (void);
49
50                         this.body = body;
51                 }
52             
53                 internal FunctionObject ()
54                 {
55                         this.parameters = new FormalParameterList ();
56                         this.body = new Block (parent);
57                 }
58
59                 public override string ToString ()
60                 {
61                         StringBuilder sb = new StringBuilder ();
62
63                         sb.Append ("function ");
64                         sb.Append (name + " ");
65                         sb.Append ("(");
66
67                         if (parameters != null)
68                                 sb.Append (this.parameters.ToString ());
69                                         
70                         sb.Append (")");
71                         sb.Append (" : " + return_type);
72                         sb.Append ("{");
73
74                         if (body != null)
75                                 sb.Append (body.ToString ());
76
77                         sb.Append ("}");
78
79                         return sb.ToString ();          
80                 }
81
82                 internal Type [] params_types ()
83                 {
84                         if (parameters == null)
85                                 return 
86                                         new Type [] {typeof (Object), typeof (Microsoft.JScript.Vsa.VsaEngine)};
87                         else {
88                                 int i, size;
89                                 ArrayList p = parameters.ids;
90
91                                 size = p.Count + 2;
92
93                                 Type [] types = new Type [size];
94
95                                 types [0] = typeof (Object);
96                                 types [1] = typeof (Microsoft.JScript.Vsa.VsaEngine);
97
98                                 for (i = 2; i < size; i++)
99                                         types [i] = ((FormalParam) p [i - 2]).type;
100
101                                 return types;
102                         }
103                 }
104         }
105 }