Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / ilasm / parser / ILParser.jay
index c7dc43180180b3a16381e1e401f5e52b5e0532bd..19a567e0ca5c2ea659d14cc67d8fbace0e18e35a 100644 (file)
@@ -10,6 +10,7 @@ using PEAPI;
 using System;\r
 using System.IO;\r
 using System.Collections;\r
+using System.Collections.Generic;\r
 using System.Globalization;\r
 using System.Reflection;\r
 using System.Security;\r
@@ -32,6 +33,7 @@ namespace Mono.ILASM {
                 private PEAPI.PInvokeAttr pinvoke_attr;\r
                 private ILTokenizer tokenizer;\r
                static int yacc_verbose_flag;\r
+               KeyValuePair<string, TypeAttr> current_extern;\r
 \r
                 class NameValuePair {\r
                         public string Name;\r
@@ -129,9 +131,8 @@ namespace Mono.ILASM {
 \r
                 private BaseTypeRef GetTypeRef (BaseTypeRef b)\r
                 {\r
-                        if (b is BaseClassRef)\r
-                                return ((BaseClassRef) b).Clone ();\r
-                        return b;\r
+                        //FIXME: Caching required.. \r
+                        return b.Clone ();\r
                 }\r
 \r
 %}\r
@@ -306,13 +307,14 @@ namespace Mono.ILASM {
 %token K_OUT\r
 %token K_OPT\r
 // %token K_LCID\r
-%token K_RETVAL\r
+//%token K_RETVAL\r
 %token K_STATIC\r
 %token K_PUBLIC\r
 %token K_PRIVATE\r
 %token K_FAMILY\r
 %token K_INITONLY\r
 %token K_RTSPECIALNAME\r
+%token K_STRICT\r
 %token K_SPECIALNAME\r
 %token K_ASSEMBLY\r
 %token K_FAMANDASSEM\r
@@ -460,6 +462,7 @@ namespace Mono.ILASM {
 // %token K_PUBLICKEY\r
 %token K_ENABLEJITTRACKING\r
 %token K_DISABLEJITOPTIMIZER\r
+%token K_RETARGETABLE\r
 %token K_PRESERVESIG\r
 %token K_BEFOREFIELDINIT\r
 %token K_ALIGNMENT\r
@@ -475,6 +478,7 @@ namespace Mono.ILASM {
 %token K_IS\r
 %token K_ON\r
 %token K_OFF\r
+%token K_FORWARDER\r
 %token K_CHARMAPERROR\r
 \r
 /* end generated */\r
@@ -768,6 +772,14 @@ formal_typar_attr  : /* EMPTY */
                           {\r
                                 $$ = new PEAPI.GenericParamAttributes ();\r
                           }\r
+                       | formal_typar_attr PLUS\r
+                          {\r
+                               $$ = (PEAPI.GenericParamAttributes) $1 | PEAPI.GenericParamAttributes.Covariant; \r
+                          }\r
+                       | formal_typar_attr DASH\r
+                          {\r
+                               $$ = (PEAPI.GenericParamAttributes) $1 | PEAPI.GenericParamAttributes.Contravariant; \r
+                          }\r
                        | formal_typar_attr D_CTOR\r
                           {\r
                                $$ = (PEAPI.GenericParamAttributes) $1 | PEAPI.GenericParamAttributes.DefaultConstructorConstrait; \r
@@ -907,8 +919,8 @@ class_decl          : method_all
                                         (CallConv) $6, (string) $4, param_list, 0);\r
 \r
                                // NOTICE: `owner' here might be wrong\r
-                                string sig = MethodDef.CreateSignature (owner, (string) $10,\r
-                                                                        param_list, 0);\r
+                                string sig = MethodDef.CreateSignature (owner, (CallConv) $6, (string) $10,\r
+                                                                        param_list, 0, false);\r
                                 codegen.CurrentTypeDef.AddOverride (sig, decl);                                        \r
                           }\r
                          OPEN_PARENS sig_args CLOSE_PARENS\r
@@ -1911,6 +1923,7 @@ meth_attr         : /* EMPTY */                   { $$ = new MethAttr (); }
                        | meth_attr K_REQSECOBJ         { $$ = (MethAttr) $1 | MethAttr.RequireSecObject; }\r
                        | meth_attr K_SPECIALNAME       { $$ = (MethAttr) $1 | MethAttr.SpecialName; }\r
                        | meth_attr K_RTSPECIALNAME     { $$ = (MethAttr) $1 | MethAttr.RTSpecialName; }\r
+                       | meth_attr K_STRICT            { $$ = (MethAttr) $1 | MethAttr.Strict; }\r
                         | meth_attr K_COMPILERCONTROLLED { /* Do nothing */ }\r
                        | meth_attr K_UNMANAGEDEXP      \r
                        | meth_attr K_PINVOKEIMPL OPEN_PARENS comp_qstring K_AS\r
@@ -2067,6 +2080,11 @@ method_decls             : /* EMPTY */
                        ;\r
 \r
 method_decl            : D_EMITBYTE int32\r
+                                               {\r
+                                                       codegen.CurrentMethodDef.AddInstr (new\r
+                                        EmitByteInstr ((int) $2, tokenizer.Location));\r
+                          \r
+                                               }\r
                        | D_MAXSTACK int32\r
                           {\r
                                 codegen.CurrentMethodDef.SetMaxStack ((int) $2);\r
@@ -2143,9 +2161,7 @@ method_decl               : D_EMITBYTE int32
 \r
                                 if (param == null) {\r
                                         Report.Warning (tokenizer.Location, String.Format ("invalid param index ({0}) with .param", index));\r
-                                        break;\r
-                                }\r
-                                if ($5 != null)\r
+                                } else if ($5 != null)\r
                                         param.AddDefaultValue ((Constant) $5);\r
                           }\r
                        | param_type_decl\r
@@ -2230,12 +2246,14 @@ scope_block             : scope_block_begin method_decls CLOSE_BRACE
                           {\r
                                 $$ = new HandlerBlock ((LabelInfo) $1,\r
                                         codegen.CurrentMethodDef.AddLabel ());\r
+                                codegen.CurrentMethodDef.EndLocalsScope ();\r
                           }\r
                        ;\r
 \r
 scope_block_begin       : OPEN_BRACE\r
                           {\r
                                 $$ = codegen.CurrentMethodDef.AddLabel ();\r
+                                codegen.CurrentMethodDef.BeginLocalsScope ();\r
                           }\r
                         ;\r
                         \r
@@ -2285,12 +2303,12 @@ seh_clauses             : seh_clause
                           }\r
                        ;\r
 \r
-seh_clause             : K_CATCH class_ref handler_block\r
+seh_clause             : K_CATCH type handler_block\r
                           {\r
                                if ($2.GetType () == typeof (PrimitiveTypeRef))\r
                                        Report.Error ("Exception not be of a primitive type.");\r
                                        \r
-                                BaseClassRef type = (BaseClassRef) $2;\r
+                                BaseTypeRef type = (BaseTypeRef) $2;\r
                                 CatchBlock cb = new CatchBlock (type);\r
                                 cb.SetHandlerBlock ((HandlerBlock) $3);\r
                                 $$ = cb;\r
@@ -2347,8 +2365,8 @@ handler_block             : scope_block
                           }\r
                        | K_HANDLER int32 K_TO int32\r
                          {\r
-                               LabelInfo from = codegen.CurrentMethodDef.AddLabelRef ((string) $2);\r
-                               LabelInfo to = codegen.CurrentMethodDef.AddLabelRef ((string) $4);\r
+                               LabelInfo from = codegen.CurrentMethodDef.AddLabel ((int) $2);\r
+                               LabelInfo to = codegen.CurrentMethodDef.AddLabel ((int) $4);\r
 \r
                                $$ = new HandlerBlock (from, to);\r
                          }\r
@@ -2367,6 +2385,8 @@ instr                     : INSTR_NONE
                         | INSTR_LOCAL id\r
                           {\r
                                 int slot = codegen.CurrentMethodDef.GetNamedLocalSlot ((string) $2);\r
+                                if (slot < 0)\r
+                                        Report.Error (String.Format ("Undeclared identifier '{0}'", (string) $2));\r
                                 codegen.CurrentMethodDef.AddInstr (\r
                                         new IntInstr ((IntOp) $1, slot, tokenizer.Location));\r
                           }\r
@@ -2392,6 +2412,8 @@ instr                     : INSTR_NONE
                        | INSTR_I id\r
                           {\r
                                 int slot = codegen.CurrentMethodDef.GetNamedLocalSlot ((string) $2);\r
+                                if (slot < 0)\r
+                                        Report.Error (String.Format ("Undeclared identifier '{0}'", (string) $2));\r
                                 codegen.CurrentMethodDef.AddInstr (new\r
                                         IntInstr ((IntOp) $1, slot, tokenizer.Location));\r
                           }\r
@@ -2428,13 +2450,23 @@ instr                   : INSTR_NONE
                           }\r
                        | INSTR_R bytes_list\r
                           {\r
+                               byte[] fpdata;\r
                                 switch ((MiscInstr) $1) {\r
                                         case MiscInstr.ldc_r4:\r
-                                                float s = BitConverter.ToSingle ((byte []) $2, 0);\r
+                                               fpdata = (byte []) $2;\r
+                                               if (!BitConverter.IsLittleEndian) {\r
+                                                       System.Array.Reverse (fpdata, 0, 4);\r
+                                               }\r
+                                                float s = BitConverter.ToSingle (fpdata, 0);\r
                                                 codegen.CurrentMethodDef.AddInstr (new LdcInstr ((MiscInstr) $1, s, tokenizer.Location));\r
                                                 break;\r
                                         case MiscInstr.ldc_r8:\r
-                                                double d = BitConverter.ToDouble ((byte []) $2, 0);\r
+                                               fpdata = (byte []) $2;\r
+                                               if (!BitConverter.IsLittleEndian) {\r
+                                                       // FIXME: handle the stupid ARM FPA format\r
+                                                       System.Array.Reverse (fpdata, 0, 8);\r
+                                               }\r
+                                                double d = BitConverter.ToDouble (fpdata, 0);\r
                                                 codegen.CurrentMethodDef.AddInstr (new LdcInstr ((MiscInstr) $1, d, tokenizer.Location));\r
                                                 break;\r
                                 }\r
@@ -2584,7 +2616,7 @@ labels                    : /* EMPTY */
                        | id\r
                           {\r
                                 ArrayList label_list = new ArrayList ();\r
-                                label_list.Add ($1);\r
+                                label_list.Add (codegen.CurrentMethodDef.AddLabelRef ((string) $1));\r
                                 $$ = label_list;\r
                           }\r
                        | int32\r
@@ -2596,7 +2628,7 @@ labels                    : /* EMPTY */
                        | labels COMMA id\r
                           {\r
                                 ArrayList label_list = (ArrayList) $1;\r
-                                label_list.Add ($3);\r
+                                label_list.Add (codegen.CurrentMethodDef.AddLabelRef ((string) $3));\r
                           }\r
                        | labels COMMA int32\r
                           {\r
@@ -2767,7 +2799,7 @@ customattr_decl           : D_CUSTOM custom_type
                          bytes_list\r
                        ;\r
                        \r
-custom_type            : call_conv type type_spec DOUBLE_COLON D_CTOR OPEN_PARENS type_list CLOSE_PARENS\r
+custom_type            : call_conv type type_spec DOUBLE_COLON method_name OPEN_PARENS type_list CLOSE_PARENS\r
                           {\r
                                 BaseTypeRef owner = (BaseTypeRef) $3;\r
                                 ArrayList arg_list = (ArrayList) $7;\r
@@ -2781,7 +2813,7 @@ custom_type               : call_conv type type_spec DOUBLE_COLON D_CTOR OPEN_PARENS type_lis
                                 $$ = owner.GetMethodRef ((BaseTypeRef) $2,\r
                                         (CallConv) $1, (string) $5, param_list, 0);\r
                           }\r
-                       | call_conv type D_CTOR OPEN_PARENS type_list CLOSE_PARENS\r
+                       | call_conv type method_name OPEN_PARENS type_list CLOSE_PARENS\r
                           {\r
                                 ArrayList arg_list = (ArrayList) $5;\r
                                 BaseTypeRef[] param_list;\r
@@ -3059,16 +3091,23 @@ assembly_all            : assembly_head OPEN_BRACE assembly_decls CLOSE_BRACE
 \r
 assembly_head          : D_ASSEMBLY asm_attr slashed_name\r
                           {\r
-                                codegen.SetAssemblyName ((string) $3);\r
+                                codegen.SetThisAssembly ((string) $3, (PEAPI.AssemAttr) $2);\r
                                 codegen.CurrentCustomAttrTarget = codegen.ThisAssembly;\r
                                codegen.CurrentDeclSecurityTarget = codegen.ThisAssembly;\r
                           }\r
                        ;\r
 \r
 asm_attr               : /* EMPTY */\r
-                       | asm_attr K_NOAPPDOMAIN\r
+                         {\r
+                                 $$ = new PEAPI.AssemAttr ();\r
+                         }\r
+                       /*| asm_attr K_NOAPPDOMAIN\r
                        | asm_attr K_NOPROCESS\r
-                       | asm_attr K_NOMACHINE\r
+                       | asm_attr K_NOMACHINE*/\r
+                       | asm_attr K_RETARGETABLE\r
+                         {\r
+                                 $$ = ((PEAPI.AssemAttr) $1) | PEAPI.AssemAttr.Retargetable;\r
+                         }\r
                        ;\r
 \r
 assembly_decls         : /* EMPTY */\r
@@ -3112,19 +3151,19 @@ asm_or_ref_decl         : D_PUBLICKEY ASSIGN bytes_list
 assemblyref_all                : assemblyref_head OPEN_BRACE assemblyref_decls CLOSE_BRACE\r
                        ;\r
 \r
-assemblyref_head       : D_ASSEMBLY K_EXTERN slashed_name\r
+assemblyref_head       : D_ASSEMBLY K_EXTERN asm_attr slashed_name\r
                           {\r
                                 System.Reflection.AssemblyName asmb_name = \r
                                        new System.Reflection.AssemblyName ();\r
-                               asmb_name.Name = (string) $3;\r
-                               codegen.BeginAssemblyRef ((string) $3, asmb_name);\r
+                               asmb_name.Name = (string) $4;\r
+                               codegen.BeginAssemblyRef ((string) $4, asmb_name, (PEAPI.AssemAttr) $3);\r
                           }\r
-                       | D_ASSEMBLY K_EXTERN slashed_name K_AS slashed_name\r
+                       | D_ASSEMBLY K_EXTERN asm_attr slashed_name K_AS slashed_name\r
                           {\r
                                 System.Reflection.AssemblyName asmb_name = \r
                                        new System.Reflection.AssemblyName ();\r
-                               asmb_name.Name = (string) $3;\r
-                               codegen.BeginAssemblyRef ((string) $5, asmb_name);\r
+                               asmb_name.Name = (string) $4;\r
+                               codegen.BeginAssemblyRef ((string) $6, asmb_name, (PEAPI.AssemAttr) $3);\r
                           }\r
                        ;\r
 \r
@@ -3165,17 +3204,21 @@ exptype_all             : exptype_head OPEN_BRACE exptype_decls CLOSE_BRACE
                        ;\r
 \r
 exptype_head           : D_CLASS K_EXTERN expt_attr comp_name\r
+                                       {\r
+                                               current_extern = new KeyValuePair<string, TypeAttr> ((string) $4, (TypeAttr) $3);\r
+                                       }\r
                        ;\r
 \r
 expt_attr              : /* EMPTY */\r
-                       | expt_attr K_PRIVATE\r
-                       | expt_attr K_PUBLIC \r
-                       | expt_attr K_NESTED K_PUBLIC\r
-                       | expt_attr K_NESTED K_PRIVATE\r
-                       | expt_attr K_NESTED K_FAMILY\r
-                       | expt_attr K_NESTED K_ASSEMBLY\r
-                       | expt_attr K_NESTED K_FAMANDASSEM\r
-                       | expt_attr K_NESTED K_FAMORASSEM\r
+                       | expt_attr K_PRIVATE                   { $$ = (TypeAttr)$1 | TypeAttr.Private; }\r
+                       | expt_attr K_PUBLIC                    { $$ = (TypeAttr)$1 | TypeAttr.Public; }\r
+                       | expt_attr K_NESTED K_PUBLIC           { $$ = (TypeAttr)$1 | TypeAttr.NestedPublic; }\r
+                       | expt_attr K_NESTED K_PRIVATE          { $$ = (TypeAttr)$1 | TypeAttr.NestedPrivate; }\r
+                       | expt_attr K_NESTED K_FAMILY           { $$ = (TypeAttr)$1 | TypeAttr.NestedFamily; }\r
+                       | expt_attr K_NESTED K_ASSEMBLY         { $$ = (TypeAttr)$1 | TypeAttr.NestedAssembly;}\r
+                       | expt_attr K_NESTED K_FAMANDASSEM      { $$ = (TypeAttr)$1 | TypeAttr.NestedFamAndAssem; }\r
+                       | expt_attr K_NESTED K_FAMORASSEM       { $$ = (TypeAttr)$1 | TypeAttr.NestedFamOrAssem; }\r
+                       | K_FORWARDER                           { $$ = TypeAttr.Forwarder; }\r
                        ;\r
 \r
 exptype_decls          : /* EMPTY */\r
@@ -3184,8 +3227,11 @@ exptype_decls            : /* EMPTY */
 \r
 exptype_decl           : D_FILE comp_name\r
                        | D_CLASS K_EXTERN comp_name\r
-                       | D_CLASS int32\r
                        | customattr_decl\r
+                       | D_ASSEMBLY K_EXTERN comp_name\r
+                         {\r
+                               codegen.ExternTable.AddClass (current_extern.Key, current_extern.Value, (string) $3);\r
+                         }\r
                        ;\r
 \r
 manifestres_all                : manifestres_head OPEN_BRACE manifestres_decls CLOSE_BRACE\r
@@ -3236,7 +3282,7 @@ float64                   : FLOAT64
                           {\r
                                 int i = (int) $3;\r
                                 byte[] intb = BitConverter.GetBytes (i);\r
-                                $$ = (double) BitConverter.ToSingle (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
+                                $$ = (double) BitConverter.ToSingle (intb, 0);\r
                           }\r
                         | K_FLOAT32 OPEN_PARENS INT64 CLOSE_PARENS\r
                           {\r
@@ -3247,12 +3293,12 @@ float64                 : FLOAT64
                        | K_FLOAT64 OPEN_PARENS INT64 CLOSE_PARENS\r
                           {\r
                                 byte[] intb = BitConverter.GetBytes ((long) $3);\r
-                               $$ = BitConverter.ToDouble (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
+                               $$ = BitConverter.ToDouble (intb, 0);\r
                           }\r
                         | K_FLOAT64 OPEN_PARENS INT32 CLOSE_PARENS\r
                           {\r
                                 byte[] intb = BitConverter.GetBytes ((int) $3);\r
-                                $$ = (double) BitConverter.ToSingle (intb, BitConverter.IsLittleEndian ? 0 : 4);\r
+                                $$ = (double) BitConverter.ToSingle (intb, 0);\r
                           }\r
                        ;\r
 \r