3 // cs-parser.jay: The Parser for the C# compiler
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Ravi Pratap (ravi@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
14 // (1) Figure out why error productions dont work. `type-declaration' is a
15 // great spot to put an `error' because you can reproduce it with this input:
18 // Possible optimization:
19 // Run memory profiler with parsing only, and consider dropping
20 // arraylists where not needed. Some pieces can use linked lists.
28 using System.Collections;
33 public class CSharpParser {
34 NamespaceEntry current_namespace;
35 TypeContainer current_container;
36 TypeContainer current_class;
38 IIteratorContainer iterator_container;
41 /// Current block is used to add statements as we find
44 Block current_block, top_current_block;
46 Delegate current_delegate;
49 /// This is used by the unary_expression code to resolve
50 /// a name against a parameter.
52 Parameters current_local_parameters;
55 /// Using during property parsing to describe the implicit
56 /// value parameter that is passed to the "set" and "get"accesor
57 /// methods (properties and indexers).
59 Expression implicit_value_parameter_type;
60 Parameters indexer_parameters;
63 /// Used to determine if we are parsing the get/set pair
64 /// of an indexer or a property
69 /// An out-of-band stack.
78 static public int yacc_verbose_flag;
80 // Name of the file we are parsing
89 /// Temporary Xml documentation cache.
90 /// For enum types, we need one more temporary store.
93 string enumTypeComment;
95 /// Current attribute target
96 string current_attr_target;
98 /// assembly and module attribute definition is enabled
99 bool global_attrs_enabled = true;
103 %token NONE /* This token is never returned by our lexer */
104 %token ERROR // This is used not by the parser, but by the tokenizer.
108 *These are the C# keywords
195 /* C# keywords which are not really keywords */
201 /* C# single character operators/punctuation. */
202 %token OPEN_BRACE "{"
203 %token CLOSE_BRACE "}"
204 %token OPEN_BRACKET "["
205 %token CLOSE_BRACKET "]"
206 %token OPEN_PARENS "("
207 %token CLOSE_PARENS ")"
219 %token OP_GENERICS_LT "<"
221 %token OP_GENERICS_GT ">"
222 %token BITWISE_AND "&"
223 %token BITWISE_OR "|"
230 /* C# multi-character operators. */
233 %token OP_SHIFT_LEFT "<<"
234 %token OP_SHIFT_RIGHT ">>"
241 %token OP_MULT_ASSIGN "*="
242 %token OP_DIV_ASSIGN "/="
243 %token OP_MOD_ASSIGN "%="
244 %token OP_ADD_ASSIGN "+="
245 %token OP_SUB_ASSIGN "-="
246 %token OP_SHIFT_LEFT_ASSIGN "<<="
247 %token OP_SHIFT_RIGHT_ASSIGN ">>="
248 %token OP_AND_ASSIGN "&="
249 %token OP_XOR_ASSIGN "^="
250 %token OP_OR_ASSIGN "|="
254 %token LITERAL_INTEGER "int literal"
255 %token LITERAL_FLOAT "float literal"
256 %token LITERAL_DOUBLE "double literal"
257 %token LITERAL_DECIMAL "decimal literal"
258 %token LITERAL_CHARACTER "character literal"
259 %token LITERAL_STRING "string literal"
262 %token CLOSE_PARENS_CAST
263 %token CLOSE_PARENS_NO_CAST
264 %token CLOSE_PARENS_OPEN_PARENS
265 %token CLOSE_PARENS_MINUS
266 %token DEFAULT_OPEN_PARENS
267 %token GENERIC_DIMENSION
269 /* Add precedence rules to solve dangling else s/r conflict */
278 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
280 %left STAR DIV PERCENT
281 %right BANG CARRET UMINUS
282 %nonassoc OP_INC OP_DEC
284 %left OPEN_BRACKET OPEN_BRACE
288 %start compilation_unit
292 : outer_declarations opt_EOF
293 | outer_declarations global_attributes opt_EOF
294 | global_attributes opt_EOF
295 | opt_EOF /* allow empty files */
301 Lexer.check_incorrect_doc_comment ();
305 Lexer.check_incorrect_doc_comment ();
311 | outer_declarations outer_declaration
316 | namespace_member_declaration
321 | using_directives using_directive
325 : using_alias_directive
327 if (RootContext.Documentation != null)
328 Lexer.doc_state = XmlCommentState.Allowed;
330 | using_namespace_directive
332 if (RootContext.Documentation != null)
333 Lexer.doc_state = XmlCommentState.Allowed;
337 using_alias_directive
338 : USING IDENTIFIER ASSIGN
339 namespace_or_type_name SEMICOLON
341 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
344 CheckIdentifierToken (yyToken);
348 using_namespace_directive
349 : USING namespace_name SEMICOLON
351 current_namespace.Using ((string) $2, lexer.Location);
356 // Strictly speaking, namespaces don't have attributes but
357 // we parse global attributes along with namespace declarations and then
360 namespace_declaration
361 : opt_attributes NAMESPACE namespace_or_type_name
364 Report.Error(1518, Lexer.Location, "Attributes cannot be applied to namespaces."
365 + " Expected class, delegate, enum, interface, or struct");
368 MemberName name = (MemberName) $3;
370 if (name.TypeArguments != null)
371 syntax_error (lexer.Location, "namespace name expected");
372 else if ((current_namespace.Parent != null) && (name.Left != null)) {
373 Report.Error (134, lexer.Location,
374 "Cannot use qualified namespace names in nested " +
375 "namespace declarations");
378 current_namespace = new NamespaceEntry (
379 current_namespace, file, name.GetName (), lexer.Location);
381 namespace_body opt_semicolon
383 current_namespace = current_namespace.Parent;
398 : namespace_or_type_name {
399 MemberName name = (MemberName) $1;
401 if (name.TypeArguments != null)
402 syntax_error (lexer.Location, "namespace name expected");
404 $$ = name.GetName ();
411 if (RootContext.Documentation != null)
412 Lexer.doc_state = XmlCommentState.Allowed;
415 opt_namespace_member_declarations
426 opt_namespace_member_declarations
428 | namespace_member_declarations
431 namespace_member_declarations
432 : namespace_member_declaration
433 | namespace_member_declarations namespace_member_declaration
436 namespace_member_declaration
443 Class c = (Class) $1;
444 mod_flags = c.ModFlags;
446 } else if ($1 is Struct){
447 Struct s = (Struct) $1;
448 mod_flags = s.ModFlags;
453 if ((mod_flags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
455 1527, lexer.Location,
456 "Namespace elements cant be explicitly " +
457 "declared private or protected in `" + name + "'");
459 current_namespace.DeclarationFound = true;
461 | namespace_declaration {
462 current_namespace.DeclarationFound = true;
465 | field_declaration {
466 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
468 | method_declaration {
469 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
476 | interface_declaration
478 | delegate_declaration
480 // Enable this when we have handled all errors, because this acts as a generic fallback
483 // Console.WriteLine ("Token=" + yyToken);
484 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
496 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
504 global_attrs_enabled = false;
509 global_attrs_enabled = false;
518 ArrayList sect = (ArrayList) $1;
520 if (global_attrs_enabled) {
521 if (current_attr_target == "module") {
522 CodeGen.Module.AddAttributes (sect);
524 } else if (current_attr_target == "assembly") {
525 CodeGen.Assembly.AddAttributes (sect);
528 $$ = new Attributes (sect);
531 if (RootContext.Documentation != null) {
532 Lexer.check_incorrect_doc_comment ();
534 XmlCommentState.Allowed;
538 $$ = new Attributes (sect);
540 current_attr_target = null;
542 | attribute_sections attribute_section
544 Attributes attrs = $1 as Attributes;
545 ArrayList sect = (ArrayList) $2;
547 if (global_attrs_enabled) {
548 if (current_attr_target == "module") {
549 CodeGen.Module.AddAttributes (sect);
551 } else if (current_attr_target == "assembly") {
552 CodeGen.Assembly.AddAttributes (sect);
556 attrs = new Attributes (sect);
558 attrs.AddAttributes (sect);
562 attrs = new Attributes (sect);
564 attrs.AddAttributes (sect);
567 current_attr_target = null;
572 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
576 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
582 attribute_target_specifier
583 : attribute_target COLON
585 current_attr_target = (string)$1;
593 CheckAttributeTarget ((string) $1);
596 | EVENT { $$ = "event"; }
597 | RETURN { $$ = "return"; }
603 ArrayList attrs = new ArrayList (4);
609 | attribute_list COMMA attribute
611 ArrayList attrs = (ArrayList) $1;
623 opt_attribute_arguments
625 MemberName mname = (MemberName) $1;
626 if (mname.IsGeneric) {
627 Report.Error (404, lexer.Location,
628 "'<' unexpected: attributes cannot be generic");
631 string name = mname.GetName ();
632 if (current_attr_target == "assembly" || current_attr_target == "module")
633 $$ = new GlobalAttribute (current_container, current_attr_target,
634 name, (ArrayList) $3, (Location) $2);
636 $$ = new Attribute (current_attr_target, name, (ArrayList) $3,
642 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
645 opt_attribute_arguments
646 : /* empty */ { $$ = null; }
647 | OPEN_PARENS attribute_arguments CLOSE_PARENS
655 : opt_positional_argument_list
660 ArrayList args = new ArrayList (4);
666 | positional_argument_list COMMA named_argument_list
668 ArrayList args = new ArrayList (4);
674 | named_argument_list
676 ArrayList args = new ArrayList (4);
685 opt_positional_argument_list
686 : /* empty */ { $$ = null; }
687 | positional_argument_list
690 positional_argument_list
693 ArrayList args = new ArrayList (4);
694 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
698 | positional_argument_list COMMA expression
700 ArrayList args = (ArrayList) $1;
701 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
710 ArrayList args = new ArrayList (4);
715 | named_argument_list COMMA named_argument
717 ArrayList args = (ArrayList) $1;
722 | named_argument_list COMMA expression
724 Report.Error (1016, lexer.Location, "Named attribute argument expected");
730 : IDENTIFIER ASSIGN expression
732 $$ = new DictionaryEntry (
734 new Argument ((Expression) $3, Argument.AType.Expression));
740 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
743 opt_class_member_declarations
745 | class_member_declarations
748 class_member_declarations
749 : class_member_declaration
750 | class_member_declarations
751 class_member_declaration
754 class_member_declaration
755 : constant_declaration // done
756 | field_declaration // done
757 | method_declaration // done
758 | property_declaration // done
759 | event_declaration // done
760 | indexer_declaration // done
761 | operator_declaration // done
762 | constructor_declaration // done
763 | destructor_declaration // done
773 MemberName name = MakeName ((MemberName) $5);
774 bool partial = (bool) $3;
777 ClassPart part = PartialContainer.CreatePart (
778 current_namespace, current_container, name, (int) $2,
779 (Attributes) $1, Kind.Struct, lexer.Location);
781 current_container = part.PartialContainer;
782 current_class = part;
784 current_class = new Struct (
785 current_namespace, current_container, name, (int) $2,
786 (Attributes) $1, lexer.Location);
788 current_container = current_class;
789 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
792 lexer.ConstraintsParsing = true;
795 opt_type_parameter_constraints_clauses
797 lexer.ConstraintsParsing = false;
800 current_class.Bases = (ArrayList) $7;
802 current_class.SetParameterInfo ((ArrayList) $8);
804 if (RootContext.Documentation != null)
805 current_class.DocComment = Lexer.consume_doc_comment ();
807 current_class.Register ();
811 if (RootContext.Documentation != null)
812 Lexer.doc_state = XmlCommentState.Allowed;
818 current_container = current_container.Parent;
819 current_class = current_container;
821 | opt_attributes opt_modifiers opt_partial STRUCT error {
822 CheckIdentifierToken (yyToken);
829 if (RootContext.Documentation != null)
830 Lexer.doc_state = XmlCommentState.Allowed;
832 opt_struct_member_declarations CLOSE_BRACE
835 opt_struct_member_declarations
837 | struct_member_declarations
840 struct_member_declarations
841 : struct_member_declaration
842 | struct_member_declarations struct_member_declaration
845 struct_member_declaration
846 : constant_declaration
849 | property_declaration
851 | indexer_declaration
852 | operator_declaration
853 | constructor_declaration
857 * This is only included so we can flag error 575:
858 * destructors only allowed on class types
860 | destructor_declaration
871 int modflags = (int) $2;
872 foreach (VariableDeclaration constant in (ArrayList) $5){
873 Location l = constant.Location;
874 if ((modflags & Modifiers.STATIC) != 0) {
875 Report.Error (504, l, "The constant '{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
879 Const c = new Const (
880 current_class, (Expression) $4, (string) constant.identifier,
881 (Expression) constant.expression_or_array_initializer, modflags,
884 if (RootContext.Documentation != null) {
885 c.DocComment = Lexer.consume_doc_comment ();
886 Lexer.doc_state = XmlCommentState.Allowed;
888 current_container.AddConstant (c);
894 : constant_declarator
896 ArrayList constants = new ArrayList (4);
901 | constant_declarators COMMA constant_declarator
904 ArrayList constants = (ArrayList) $1;
911 : IDENTIFIER ASSIGN constant_expression
913 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
917 // A const field requires a value to be provided
918 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
930 Expression type = (Expression) $3;
933 foreach (VariableDeclaration var in (ArrayList) $4){
934 Location l = var.Location;
936 Field field = new Field (current_class, type, mod, var.identifier,
937 var.expression_or_array_initializer,
940 if (RootContext.Documentation != null) {
941 field.DocComment = Lexer.consume_doc_comment ();
942 Lexer.doc_state = XmlCommentState.Allowed;
944 current_container.AddField (field);
952 Report.Error (670, lexer.Location, "void type is not allowed for fields");
957 : variable_declarator
959 ArrayList decl = new ArrayList (4);
963 | variable_declarators COMMA variable_declarator
965 ArrayList decls = (ArrayList) $1;
972 : IDENTIFIER ASSIGN variable_initializer
974 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
978 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
991 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
993 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
997 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1004 iterator_container = (IIteratorContainer) $1;
1005 if (RootContext.Documentation != null)
1006 Lexer.doc_state = XmlCommentState.NotAllowed;
1010 Method method = (Method) $1;
1011 Block b = (Block) $3;
1012 const int extern_abstract = (Modifiers.EXTERN | Modifiers.ABSTRACT);
1015 if ((method.ModFlags & extern_abstract) == 0){
1017 501, lexer.Location, current_container.MakeName (method.Name) +
1018 "must declare a body because it is not marked abstract or extern");
1021 if ((method.ModFlags & Modifiers.EXTERN) != 0){
1023 179, lexer.Location, current_container.MakeName (method.Name) +
1024 " is declared extern, but has a body");
1028 method.Block = (ToplevelBlock) $3;
1029 current_container.AddMethod (method);
1031 current_local_parameters = null;
1032 iterator_container = null;
1034 if (RootContext.Documentation != null)
1035 Lexer.doc_state = XmlCommentState.Allowed;
1049 1585, lexer.Location, "Member modifier `" +
1050 Modifiers.Name (i) + "' must precede member type and name");
1061 type namespace_or_type_name
1062 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1064 lexer.ConstraintsParsing = true;
1066 opt_type_parameter_constraints_clauses
1068 lexer.ConstraintsParsing = false;
1070 MemberName name = (MemberName) $4;
1072 if ($9 != null && name.TypeArguments == null)
1073 Report.Error (80, lexer.Location,
1074 "Contraints are not allowed on non-generic declarations");
1078 GenericMethod generic = null;
1079 if (name.TypeArguments != null) {
1080 generic = new GenericMethod (current_namespace, current_class,
1081 name, lexer.Location);
1083 generic.SetParameterInfo ((ArrayList) $9);
1086 method = new Method (current_class, generic, (Expression) $3, (int) $2, false,
1087 name, (Parameters) $6, (Attributes) $1, lexer.Location);
1089 current_local_parameters = (Parameters) $6;
1091 if (RootContext.Documentation != null)
1092 method.DocComment = Lexer.consume_doc_comment ();
1098 VOID namespace_or_type_name
1099 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1101 lexer.ConstraintsParsing = true;
1103 opt_type_parameter_constraints_clauses
1105 lexer.ConstraintsParsing = false;
1107 MemberName name = (MemberName) $4;
1109 if ($9 != null && name.TypeArguments == null)
1110 Report.Error (80, lexer.Location,
1111 "Contraints are not allowed on non-generic declarations");
1114 GenericMethod generic = null;
1115 if (name.TypeArguments != null) {
1116 generic = new GenericMethod (current_namespace, current_class,
1117 name, lexer.Location);
1119 generic.SetParameterInfo ((ArrayList) $9);
1122 method = new Method (current_class, generic, TypeManager.system_void_expr,
1123 (int) $2, false, name, (Parameters) $6, (Attributes) $1,
1126 current_local_parameters = (Parameters) $6;
1128 if (RootContext.Documentation != null)
1129 method.DocComment = Lexer.consume_doc_comment ();
1136 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1138 Report.Error (1585, lexer.Location,
1139 String.Format ("Modifier {0} should appear before type",
1140 Modifiers.Name ((int) $4)));
1141 MemberName name = (MemberName) $4;
1143 Method method = new Method (current_class, null, TypeManager.system_void_expr,
1144 0, false, name, (Parameters) $6, (Attributes) $1,
1147 current_local_parameters = (Parameters) $6;
1149 if (RootContext.Documentation != null)
1150 method.DocComment = Lexer.consume_doc_comment ();
1158 | SEMICOLON { $$ = null; }
1161 opt_formal_parameter_list
1162 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1163 | formal_parameter_list
1166 formal_parameter_list
1169 ArrayList pars_list = (ArrayList) $1;
1171 Parameter [] pars = new Parameter [pars_list.Count];
1172 pars_list.CopyTo (pars);
1174 $$ = new Parameters (pars, null, lexer.Location);
1176 | fixed_parameters COMMA parameter_array
1178 ArrayList pars_list = (ArrayList) $1;
1180 Parameter [] pars = new Parameter [pars_list.Count];
1181 pars_list.CopyTo (pars);
1183 $$ = new Parameters (pars, (Parameter) $3, lexer.Location);
1185 | fixed_parameters COMMA ARGLIST
1187 ArrayList pars_list = (ArrayList) $1;
1189 Parameter [] pars = new Parameter [pars_list.Count];
1190 pars_list.CopyTo (pars);
1192 $$ = new Parameters (pars, true, lexer.Location);
1196 $$ = new Parameters (null, (Parameter) $1, lexer.Location);
1200 $$ = new Parameters (null, true, lexer.Location);
1207 ArrayList pars = new ArrayList (4);
1212 | fixed_parameters COMMA fixed_parameter
1214 ArrayList pars = (ArrayList) $1;
1223 opt_parameter_modifier
1227 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1);
1230 opt_parameter_modifier
1233 Report.Error (1001, lexer.Location, "Identifier expected");
1237 opt_parameter_modifier
1240 CheckIdentifierToken (yyToken);
1244 opt_parameter_modifier
1250 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1255 opt_parameter_modifier
1256 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1257 | parameter_modifier
1261 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1262 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1266 : opt_attributes PARAMS type IDENTIFIER
1268 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1);
1269 note ("type must be a single-dimension array type");
1271 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1273 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1276 | opt_attributes PARAMS type error {
1277 CheckIdentifierToken (yyToken);
1282 property_declaration
1286 namespace_or_type_name
1288 if (RootContext.Documentation != null)
1289 tmpComment = Lexer.consume_doc_comment ();
1293 implicit_value_parameter_type = (Expression) $3;
1295 lexer.PropertyParsing = true;
1297 $$ = lexer.Location;
1299 iterator_container = SimpleIteratorContainer.GetSimple ();
1301 accessor_declarations
1303 lexer.PropertyParsing = false;
1308 Pair pair = (Pair) $8;
1309 Accessor get_block = (Accessor) pair.First;
1310 Accessor set_block = (Accessor) pair.Second;
1312 Location loc = (Location) $7;
1313 MemberName name = (MemberName) $4;
1315 if (name.TypeArguments != null)
1316 syntax_error (lexer.Location, "a property can't have type arguments");
1318 prop = new Property (current_container, (Expression) $3, (int) $2, false,
1319 name, (Attributes) $1, get_block, set_block, loc);
1320 if (SimpleIteratorContainer.Simple.Yields)
1323 current_container.AddProperty (prop);
1324 implicit_value_parameter_type = null;
1325 iterator_container = null;
1327 if (RootContext.Documentation != null)
1328 prop.DocComment = ConsumeStoredComment ();
1333 accessor_declarations
1334 : get_accessor_declaration opt_set_accessor_declaration
1336 $$ = new Pair ($1, $2);
1338 | set_accessor_declaration opt_get_accessor_declaration
1340 $$ = new Pair ($2, $1);
1344 opt_get_accessor_declaration
1345 : /* empty */ { $$ = null; }
1346 | get_accessor_declaration
1349 opt_set_accessor_declaration
1350 : /* empty */ { $$ = null; }
1351 | set_accessor_declaration
1354 get_accessor_declaration
1355 : opt_attributes opt_modifiers GET
1357 // If this is not the case, then current_local_parameters has already
1358 // been set in indexer_declaration
1359 if (parsing_indexer == false)
1360 current_local_parameters = null;
1362 current_local_parameters = indexer_parameters;
1363 lexer.PropertyParsing = false;
1367 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1368 current_local_parameters = null;
1369 lexer.PropertyParsing = true;
1371 if (RootContext.Documentation != null)
1372 if (Lexer.doc_state == XmlCommentState.Error)
1373 Lexer.doc_state = XmlCommentState.NotAllowed;
1377 set_accessor_declaration
1378 : opt_attributes opt_modifiers SET
1381 Parameter implicit_value_parameter = new Parameter (
1382 implicit_value_parameter_type, "value",
1383 Parameter.Modifier.NONE, null);
1385 if (parsing_indexer == false) {
1386 args = new Parameter [1];
1387 args [0] = implicit_value_parameter;
1388 current_local_parameters = new Parameters (args, null, lexer.Location);
1390 Parameter [] fpars = indexer_parameters.FixedParameters;
1393 int count = fpars.Length;
1395 args = new Parameter [count + 1];
1396 fpars.CopyTo (args, 0);
1397 args [count] = implicit_value_parameter;
1400 current_local_parameters = new Parameters (
1401 args, indexer_parameters.ArrayParameter, lexer.Location);
1404 lexer.PropertyParsing = false;
1408 $$ = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1409 current_local_parameters = null;
1410 lexer.PropertyParsing = true;
1412 if (RootContext.Documentation != null
1413 && Lexer.doc_state == XmlCommentState.Error)
1414 Lexer.doc_state = XmlCommentState.NotAllowed;
1420 | SEMICOLON { $$ = null; }
1423 interface_declaration
1427 INTERFACE member_name
1429 MemberName name = MakeName ((MemberName) $5);
1430 bool partial = (bool) $3;
1433 ClassPart part = PartialContainer.CreatePart (
1434 current_namespace, current_container, name, (int) $2,
1435 (Attributes) $1, Kind.Interface, lexer.Location);
1437 current_container = part.PartialContainer;
1438 current_class = part;
1440 current_class = new Interface (
1441 current_namespace, current_container, name, (int) $2,
1442 (Attributes) $1, lexer.Location);
1444 current_container = current_class;
1445 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
1448 lexer.ConstraintsParsing = true;
1451 opt_type_parameter_constraints_clauses
1453 lexer.ConstraintsParsing = false;
1456 current_class.Bases = (ArrayList) $7;
1458 current_class.SetParameterInfo ((ArrayList) $8);
1460 if (RootContext.Documentation != null) {
1461 current_class.DocComment = Lexer.consume_doc_comment ();
1462 Lexer.doc_state = XmlCommentState.Allowed;
1465 current_class.Register ();
1469 if (RootContext.Documentation != null)
1470 Lexer.doc_state = XmlCommentState.Allowed;
1476 current_container = current_container.Parent;
1477 current_class = current_container;
1479 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1480 CheckIdentifierToken (yyToken);
1486 opt_interface_member_declarations
1490 opt_interface_member_declarations
1492 | interface_member_declarations
1495 interface_member_declarations
1496 : interface_member_declaration
1497 | interface_member_declarations interface_member_declaration
1500 interface_member_declaration
1501 : interface_method_declaration
1503 Method m = (Method) $1;
1505 if (m.IsExplicitImpl)
1506 Report.Error (541, lexer.Location,
1507 "Explicit interface declaration can only be declared in a class or struct");
1509 current_container.AddMethod (m);
1511 if (RootContext.Documentation != null)
1512 Lexer.doc_state = XmlCommentState.Allowed;
1514 | interface_property_declaration
1516 Property p = (Property) $1;
1518 if (p.IsExplicitImpl)
1519 Report.Error (541, lexer.Location,
1520 "Explicit interface declaration can only be declared in a class or struct");
1522 current_container.AddProperty (p);
1524 if (RootContext.Documentation != null)
1525 Lexer.doc_state = XmlCommentState.Allowed;
1527 | interface_event_declaration
1530 Event e = (Event) $1;
1532 if (e.IsExplicitImpl)
1533 Report.Error (541, lexer.Location,
1534 "Explicit interface declaration can only be declared in a class or struct");
1536 current_container.AddEvent (e);
1539 if (RootContext.Documentation != null)
1540 Lexer.doc_state = XmlCommentState.Allowed;
1542 | interface_indexer_declaration
1544 Indexer i = (Indexer) $1;
1546 if (i.IsExplicitImpl)
1547 Report.Error (541, lexer.Location,
1548 "Explicit interface declaration can only be declared in a class or struct");
1550 current_container.AddIndexer (i);
1552 if (RootContext.Documentation != null)
1553 Lexer.doc_state = XmlCommentState.Allowed;
1561 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1566 interface_method_declaration
1567 : opt_attributes opt_new type namespace_or_type_name
1568 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1570 lexer.ConstraintsParsing = true;
1572 opt_type_parameter_constraints_clauses
1575 lexer.ConstraintsParsing = false;
1577 MemberName name = (MemberName) $4;
1579 if ($9 != null && name.TypeArguments == null)
1580 Report.Error (80, lexer.Location,
1581 "Contraints are not allowed on non-generic declarations");
1583 GenericMethod generic = null;
1584 if (name.TypeArguments != null) {
1585 generic = new GenericMethod (current_namespace, current_class,
1586 name, lexer.Location);
1588 generic.SetParameterInfo ((ArrayList) $9);
1591 $$ = new Method (current_class, generic, (Expression) $3, (int) $2, true, name,
1592 (Parameters) $6, (Attributes) $1, lexer.Location);
1593 if (RootContext.Documentation != null)
1594 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1596 | opt_attributes opt_new VOID namespace_or_type_name
1597 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1599 lexer.ConstraintsParsing = true;
1601 opt_type_parameter_constraints_clauses
1604 lexer.ConstraintsParsing = false;
1606 MemberName name = (MemberName) $4;
1608 if ($9 != null && name.TypeArguments == null)
1609 Report.Error (80, lexer.Location,
1610 "Contraints are not allowed on non-generic declarations");
1612 GenericMethod generic = null;
1613 if (name.TypeArguments != null) {
1614 generic = new GenericMethod (current_namespace, current_class,
1615 name, lexer.Location);
1617 generic.SetParameterInfo ((ArrayList) $9);
1620 $$ = new Method (current_class, generic, TypeManager.system_void_expr, (int) $2,
1621 true, name, (Parameters) $6, (Attributes) $1, lexer.Location);
1622 if (RootContext.Documentation != null)
1623 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1627 interface_property_declaration
1632 { lexer.PropertyParsing = true; }
1634 { lexer.PropertyParsing = false; }
1637 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1639 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1640 new MemberName ((string) $4), (Attributes) $1,
1641 pinfo.Get, pinfo.Set, lexer.Location);
1642 if (RootContext.Documentation != null)
1643 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1648 CheckIdentifierToken (yyToken);
1654 : opt_attributes opt_modifiers GET SEMICOLON
1655 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1656 | opt_attributes opt_modifiers SET SEMICOLON
1657 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1658 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1659 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1660 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1661 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1664 interface_event_declaration
1665 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1667 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1668 new MemberName ((string) $5), null,
1669 (Attributes) $1, lexer.Location);
1670 if (RootContext.Documentation != null)
1671 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1673 | opt_attributes opt_new EVENT type error {
1674 CheckIdentifierToken (yyToken);
1677 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1678 Report.Error (68, lexer.Location, "Event declarations on interfaces can not be initialized.");
1681 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE event_accessor_declarations CLOSE_BRACE {
1682 Report.Error (69, lexer.Location, "Event accessors not valid on interfaces");
1687 interface_indexer_declaration
1688 : opt_attributes opt_new type THIS
1689 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1691 { lexer.PropertyParsing = true; }
1693 { lexer.PropertyParsing = false; }
1696 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1698 $$ = new Indexer (current_class, (Expression) $3,
1699 new MemberName (TypeContainer.DefaultIndexerName),
1700 (int) $2, true, (Parameters) $6, (Attributes) $1,
1701 info.Get, info.Set, lexer.Location);
1702 if (RootContext.Documentation != null)
1703 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1707 operator_declaration
1708 : opt_attributes opt_modifiers operator_declarator
1710 iterator_container = SimpleIteratorContainer.GetSimple ();
1714 OperatorDeclaration decl = (OperatorDeclaration) $3;
1716 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1718 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null);
1719 if (decl.arg2type != null)
1720 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null);
1722 Operator op = new Operator (
1723 current_class, decl.optype, decl.ret_type, (int) $2,
1724 new Parameters (param_list, null, decl.location),
1725 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1727 if (RootContext.Documentation != null) {
1728 op.DocComment = tmpComment;
1729 Lexer.doc_state = XmlCommentState.Allowed;
1732 if (SimpleIteratorContainer.Simple.Yields)
1735 // Note again, checking is done in semantic analysis
1736 current_container.AddOperator (op);
1738 current_local_parameters = null;
1739 iterator_container = null;
1745 | SEMICOLON { $$ = null; }
1748 : type OPERATOR overloadable_operator
1749 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1751 Operator.OpType op = (Operator.OpType) $3;
1752 CheckUnaryOperator (op);
1754 if (op == Operator.OpType.Addition)
1755 op = Operator.OpType.UnaryPlus;
1757 if (op == Operator.OpType.Subtraction)
1758 op = Operator.OpType.UnaryNegation;
1760 Parameter [] pars = new Parameter [1];
1761 Expression type = (Expression) $5;
1763 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null);
1765 current_local_parameters = new Parameters (pars, null, lexer.Location);
1767 if (RootContext.Documentation != null) {
1768 tmpComment = Lexer.consume_doc_comment ();
1769 Lexer.doc_state = XmlCommentState.NotAllowed;
1772 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1773 null, null, lexer.Location);
1775 | type OPERATOR overloadable_operator
1777 type IDENTIFIER COMMA
1781 CheckBinaryOperator ((Operator.OpType) $3);
1783 Parameter [] pars = new Parameter [2];
1785 Expression typeL = (Expression) $5;
1786 Expression typeR = (Expression) $8;
1788 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null);
1789 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null);
1791 current_local_parameters = new Parameters (pars, null, lexer.Location);
1793 if (RootContext.Documentation != null) {
1794 tmpComment = Lexer.consume_doc_comment ();
1795 Lexer.doc_state = XmlCommentState.NotAllowed;
1798 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1800 typeR, (string) $9, lexer.Location);
1802 | conversion_operator_declarator
1805 overloadable_operator
1807 : BANG { $$ = Operator.OpType.LogicalNot; }
1808 | TILDE { $$ = Operator.OpType.OnesComplement; }
1809 | OP_INC { $$ = Operator.OpType.Increment; }
1810 | OP_DEC { $$ = Operator.OpType.Decrement; }
1811 | TRUE { $$ = Operator.OpType.True; }
1812 | FALSE { $$ = Operator.OpType.False; }
1813 // Unary and binary:
1814 | PLUS { $$ = Operator.OpType.Addition; }
1815 | MINUS { $$ = Operator.OpType.Subtraction; }
1817 | STAR { $$ = Operator.OpType.Multiply; }
1818 | DIV { $$ = Operator.OpType.Division; }
1819 | PERCENT { $$ = Operator.OpType.Modulus; }
1820 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1821 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1822 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1823 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1824 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1825 | OP_EQ { $$ = Operator.OpType.Equality; }
1826 | OP_NE { $$ = Operator.OpType.Inequality; }
1827 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1828 | OP_LT { $$ = Operator.OpType.LessThan; }
1829 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1830 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1833 conversion_operator_declarator
1834 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1836 Parameter [] pars = new Parameter [1];
1838 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1840 current_local_parameters = new Parameters (pars, null, lexer.Location);
1842 if (RootContext.Documentation != null) {
1843 tmpComment = Lexer.consume_doc_comment ();
1844 Lexer.doc_state = XmlCommentState.NotAllowed;
1847 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1848 null, null, lexer.Location);
1850 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1852 Parameter [] pars = new Parameter [1];
1854 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null);
1856 current_local_parameters = new Parameters (pars, null, lexer.Location);
1858 if (RootContext.Documentation != null) {
1859 tmpComment = Lexer.consume_doc_comment ();
1860 Lexer.doc_state = XmlCommentState.NotAllowed;
1863 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1864 null, null, lexer.Location);
1868 syntax_error (lexer.Location, "'operator' expected");
1872 syntax_error (lexer.Location, "'operator' expected");
1876 constructor_declaration
1879 constructor_declarator
1882 Constructor c = (Constructor) $3;
1883 c.Block = (ToplevelBlock) $4;
1884 c.OptAttributes = (Attributes) $1;
1885 c.ModFlags = (int) $2;
1887 if (RootContext.Documentation != null)
1888 c.DocComment = ConsumeStoredComment ();
1890 if (c.Name == current_container.Basename){
1891 if ((c.ModFlags & Modifiers.STATIC) != 0){
1892 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1894 515, c.Location, String.Format (
1895 "`{0}.{1}': static constructor can not have access modifiers",
1896 c.Name, current_container.Name));
1899 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1901 if (c.Initializer != null){
1904 "Static constructors can not have an explicit this or base " +
1905 "constructor invocations");
1908 if (!c.Parameters.Empty){
1910 132, c.Location, "Static constructors should not have parameters");
1913 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1916 // We let another layer check the validity of the constructor.
1917 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1920 current_container.AddConstructor (c);
1922 current_local_parameters = null;
1923 if (RootContext.Documentation != null)
1924 Lexer.doc_state = XmlCommentState.Allowed;
1928 constructor_declarator
1931 if (RootContext.Documentation != null) {
1932 tmpComment = Lexer.consume_doc_comment ();
1933 Lexer.doc_state = XmlCommentState.Allowed;
1936 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1938 oob_stack.Push (lexer.Location);
1940 current_local_parameters = (Parameters) $4;
1942 opt_constructor_initializer
1944 Location l = (Location) oob_stack.Pop ();
1945 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1946 (ConstructorInitializer) $7, l);
1952 | SEMICOLON { $$ = null; }
1955 opt_constructor_initializer
1956 : /* empty */ { $$ = null; }
1957 | constructor_initializer
1960 constructor_initializer
1961 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
1963 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1965 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
1967 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
1970 Report.Error (1018, lexer.Location, "Keyword this or base expected");
1976 : /* EMPTY */ { $$ = 0; }
1977 | UNSAFE { $$ = Modifiers.UNSAFE; }
1978 | EXTERN { $$ = Modifiers.EXTERN; }
1981 destructor_declaration
1982 : opt_attributes opt_finalizer TILDE
1984 if (RootContext.Documentation != null) {
1985 tmpComment = Lexer.consume_doc_comment ();
1986 Lexer.doc_state = XmlCommentState.NotAllowed;
1989 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
1991 if ((string) $5 != current_container.Basename){
1992 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
1993 } else if (current_container.Kind != Kind.Class){
1994 Report.Error (575, lexer.Location, "Destructors are only allowed in class types");
1996 Location l = lexer.Location;
1999 if (!RootContext.StdLib && current_container.Name == "System.Object")
2000 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2002 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2004 if ((m & Modifiers.UNSAFE) != 0){
2005 if (!RootContext.Unsafe){
2006 Report.Error (227, l,
2007 "Unsafe code requires the -unsafe command " +
2008 "line option to be specified");
2012 Method d = new Destructor (
2013 current_class, TypeManager.system_void_expr, m, "Finalize",
2014 new Parameters (null, null, l), (Attributes) $1, l);
2015 if (RootContext.Documentation != null)
2016 d.DocComment = ConsumeStoredComment ();
2018 d.Block = (ToplevelBlock) $8;
2019 current_container.AddMethod (d);
2027 EVENT type variable_declarators SEMICOLON
2029 foreach (VariableDeclaration var in (ArrayList) $5) {
2031 MemberName name = new MemberName (var.identifier);
2033 Event e = new EventField (
2034 current_class, (Expression) $4, (int) $2, false, name,
2035 var.expression_or_array_initializer, (Attributes) $1,
2038 current_container.AddEvent (e);
2040 if (RootContext.Documentation != null) {
2041 e.DocComment = Lexer.consume_doc_comment ();
2042 Lexer.doc_state = XmlCommentState.Allowed;
2048 EVENT type namespace_or_type_name
2051 implicit_value_parameter_type = (Expression) $4;
2052 lexer.EventParsing = true;
2053 oob_stack.Push (lexer.Location);
2055 event_accessor_declarations
2057 lexer.EventParsing = false;
2061 Location loc = (Location) oob_stack.Pop ();
2064 Report.Error (65, lexer.Location, "Event must have both add and remove accesors");
2067 Pair pair = (Pair) $8;
2069 MemberName name = (MemberName) $5;
2071 if (name.TypeArguments != null)
2072 syntax_error (lexer.Location, "an event can't have type arguments");
2074 Event e = new EventProperty (
2075 current_class, (Expression) $4, (int) $2, false, name, null,
2076 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2078 if (RootContext.Documentation != null) {
2079 e.DocComment = Lexer.consume_doc_comment ();
2080 Lexer.doc_state = XmlCommentState.Allowed;
2083 current_container.AddEvent (e);
2084 implicit_value_parameter_type = null;
2087 | opt_attributes opt_modifiers EVENT type namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS block {
2088 MemberName mn = (MemberName) $5;
2090 if (mn.Left != null)
2091 Report.Error (71, lexer.Location, "Explicit implementation of events requires property syntax");
2093 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2095 if (RootContext.Documentation != null)
2096 Lexer.doc_state = XmlCommentState.Allowed;
2100 event_accessor_declarations
2101 : add_accessor_declaration remove_accessor_declaration
2103 $$ = new Pair ($1, $2);
2105 | remove_accessor_declaration add_accessor_declaration
2107 $$ = new Pair ($2, $1);
2109 | add_accessor_declaration { $$ = null; }
2110 | remove_accessor_declaration { $$ = null; }
2113 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2119 add_accessor_declaration
2120 : opt_attributes ADD
2122 Parameter [] args = new Parameter [1];
2123 Parameter implicit_value_parameter = new Parameter (
2124 implicit_value_parameter_type, "value",
2125 Parameter.Modifier.NONE, null);
2127 args [0] = implicit_value_parameter;
2129 current_local_parameters = new Parameters (args, null, lexer.Location);
2130 lexer.EventParsing = false;
2134 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2135 lexer.EventParsing = true;
2137 | opt_attributes ADD error {
2138 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2143 remove_accessor_declaration
2144 : opt_attributes REMOVE
2146 Parameter [] args = new Parameter [1];
2147 Parameter implicit_value_parameter = new Parameter (
2148 implicit_value_parameter_type, "value",
2149 Parameter.Modifier.NONE, null);
2151 args [0] = implicit_value_parameter;
2153 current_local_parameters = new Parameters (args, null, lexer.Location);
2154 lexer.EventParsing = false;
2158 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2159 lexer.EventParsing = true;
2161 | opt_attributes REMOVE error {
2162 Report.Error (73, lexer.Location, "Add or remove accessor must have a body");
2168 : opt_attributes opt_modifiers indexer_declarator
2171 IndexerDeclaration decl = (IndexerDeclaration) $3;
2173 implicit_value_parameter_type = decl.type;
2175 lexer.PropertyParsing = true;
2176 parsing_indexer = true;
2178 indexer_parameters = decl.param_list;
2179 oob_stack.Push (lexer.Location);
2181 accessor_declarations
2183 lexer.PropertyParsing = false;
2184 parsing_indexer = false;
2188 // The signature is computed from the signature of the indexer. Look
2189 // at section 3.6 on the spec
2190 Location loc = (Location) oob_stack.Pop ();
2192 IndexerDeclaration decl = (IndexerDeclaration) $3;
2193 Pair pair = (Pair) $6;
2194 Accessor get_block = (Accessor) pair.First;
2195 Accessor set_block = (Accessor) pair.Second;
2198 if (decl.interface_type != null)
2199 name = new MemberName (decl.interface_type,
2200 TypeContainer.DefaultIndexerName, null);
2202 name = new MemberName (TypeContainer.DefaultIndexerName);
2204 indexer = new Indexer (current_class, decl.type, name,
2205 (int) $2, false, decl.param_list, (Attributes) $1,
2206 get_block, set_block, loc);
2207 if (RootContext.Documentation != null)
2208 indexer.DocComment = ConsumeStoredComment ();
2210 current_container.AddIndexer (indexer);
2212 current_local_parameters = null;
2213 implicit_value_parameter_type = null;
2214 indexer_parameters = null;
2219 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2221 Parameters pars = (Parameters) $4;
2222 if (pars.HasArglist) {
2223 // "__arglist is not valid in this context"
2224 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2225 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2226 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2228 if (RootContext.Documentation != null) {
2229 tmpComment = Lexer.consume_doc_comment ();
2230 Lexer.doc_state = XmlCommentState.Allowed;
2233 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2235 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2237 Parameters pars = (Parameters) $6;
2239 if (pars.HasArglist) {
2240 // "__arglist is not valid in this context"
2241 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2242 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2243 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2246 MemberName name = (MemberName) $2;
2247 if (name.TypeArguments != null)
2248 syntax_error (lexer.Location, "an indexer can't have type arguments");
2250 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2252 if (RootContext.Documentation != null) {
2253 tmpComment = Lexer.consume_doc_comment ();
2254 Lexer.doc_state = XmlCommentState.Allowed;
2264 if (RootContext.Documentation != null)
2265 enumTypeComment = Lexer.consume_doc_comment ();
2270 Location enum_location = lexer.Location;
2272 MemberName full_name = MakeName (new MemberName ((string) $4));
2273 Enum e = new Enum (current_namespace, current_container, (Expression) $5, (int) $2,
2274 full_name, (Attributes) $1, enum_location);
2276 if (RootContext.Documentation != null)
2277 e.DocComment = enumTypeComment;
2279 foreach (VariableDeclaration ev in (ArrayList) $7) {
2280 e.AddEnumMember (ev.identifier,
2281 (Expression) ev.expression_or_array_initializer,
2282 ev.Location, ev.OptAttributes,
2286 string name = full_name.GetName ();
2287 current_container.AddEnum (e);
2288 RootContext.Tree.RecordDecl (name, e);
2294 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2295 | COLON type { $$ = $2; }
2301 if (RootContext.Documentation != null)
2302 Lexer.doc_state = XmlCommentState.Allowed;
2304 opt_enum_member_declarations
2306 // here will be evaluated after CLOSE_BLACE is consumed.
2307 if (RootContext.Documentation != null)
2308 Lexer.doc_state = XmlCommentState.Allowed;
2316 opt_enum_member_declarations
2317 : /* empty */ { $$ = new ArrayList (4); }
2318 | enum_member_declarations opt_comma { $$ = $1; }
2321 enum_member_declarations
2322 : enum_member_declaration
2324 ArrayList l = new ArrayList (4);
2329 | enum_member_declarations COMMA enum_member_declaration
2331 ArrayList l = (ArrayList) $1;
2339 enum_member_declaration
2340 : opt_attributes IDENTIFIER
2342 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2344 if (RootContext.Documentation != null) {
2345 vd.DocComment = Lexer.consume_doc_comment ();
2346 Lexer.doc_state = XmlCommentState.Allowed;
2351 | opt_attributes IDENTIFIER
2353 $$ = lexer.Location;
2354 if (RootContext.Documentation != null) {
2355 tmpComment = Lexer.consume_doc_comment ();
2356 Lexer.doc_state = XmlCommentState.NotAllowed;
2361 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2363 if (RootContext.Documentation != null)
2364 vd.DocComment = ConsumeStoredComment ();
2370 delegate_declaration
2373 DELEGATE type member_name
2374 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2376 Location l = lexer.Location;
2377 MemberName name = MakeName ((MemberName) $5);
2378 Delegate del = new Delegate (current_namespace, current_container, (Expression) $4,
2379 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2381 if (RootContext.Documentation != null) {
2382 del.DocComment = Lexer.consume_doc_comment ();
2383 Lexer.doc_state = XmlCommentState.Allowed;
2386 current_container.AddDelegate (del);
2387 RootContext.Tree.RecordDecl (name.GetName (true), del);
2389 current_delegate = del;
2391 lexer.ConstraintsParsing = true;
2393 opt_type_parameter_constraints_clauses
2395 lexer.ConstraintsParsing = false;
2399 current_delegate.SetParameterInfo ((ArrayList) $9);
2401 current_delegate = null;
2405 DELEGATE VOID member_name
2406 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2408 Location l = lexer.Location;
2409 MemberName name = MakeName ((MemberName) $5);
2410 Delegate del = new Delegate (
2411 current_namespace, current_container,
2412 TypeManager.system_void_expr, (int) $2, name,
2413 (Parameters) $7, (Attributes) $1, l);
2415 if (RootContext.Documentation != null) {
2416 del.DocComment = Lexer.consume_doc_comment ();
2417 Lexer.doc_state = XmlCommentState.Allowed;
2420 current_container.AddDelegate (del);
2421 RootContext.Tree.RecordDecl (name.GetName (true), del);
2423 current_delegate = del;
2425 lexer.ConstraintsParsing = true;
2427 opt_type_parameter_constraints_clauses
2429 lexer.ConstraintsParsing = false;
2433 current_delegate.SetParameterInfo ((ArrayList) $9);
2435 current_delegate = null;
2439 namespace_or_type_name
2441 | namespace_or_type_name DOT IDENTIFIER opt_type_argument_list {
2442 $$ = new MemberName ((MemberName) $1, (string) $3, (TypeArguments) $4);
2447 : IDENTIFIER opt_type_argument_list {
2448 $$ = new MemberName ((string) $1, (TypeArguments) $2);
2452 opt_type_argument_list
2453 : /* empty */ { $$ = null; }
2454 | OP_GENERICS_LT type_arguments OP_GENERICS_GT
2460 $$ = new TypeArguments ((int) $1, lexer.Location);
2466 TypeArguments type_args = new TypeArguments (lexer.Location);
2467 type_args.Add ((Expression) $1);
2470 | type_arguments COMMA type {
2471 TypeArguments type_args = (TypeArguments) $1;
2472 type_args.Add ((Expression) $3);
2478 * Before you think of adding a return_type, notice that we have been
2479 * using two rules in the places where it matters (one rule using type
2480 * and another identical one that uses VOID as the return type). This
2481 * gets rid of a shift/reduce couple
2484 : namespace_or_type_name
2486 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2498 // Note that here only unmanaged types are allowed but we
2499 // can't perform checks during this phase - we do it during
2500 // semantic analysis.
2502 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2506 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2512 | non_expression_type rank_specifier
2514 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2516 | non_expression_type STAR
2518 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2520 | expression rank_specifiers
2522 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2526 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2530 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2531 // through this different path
2533 | multiplicative_expression STAR
2535 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2542 ArrayList types = new ArrayList (4);
2547 | type_list COMMA type
2549 ArrayList types = (ArrayList) $1;
2557 * replaces all the productions for isolating the various
2558 * simple types, but we need this to reuse it easily in local_variable_type
2561 : OBJECT { $$ = TypeManager.system_object_expr; }
2562 | STRING { $$ = TypeManager.system_string_expr; }
2563 | BOOL { $$ = TypeManager.system_boolean_expr; }
2564 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2565 | FLOAT { $$ = TypeManager.system_single_expr; }
2566 | DOUBLE { $$ = TypeManager.system_double_expr; }
2571 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2572 | BYTE { $$ = TypeManager.system_byte_expr; }
2573 | SHORT { $$ = TypeManager.system_int16_expr; }
2574 | USHORT { $$ = TypeManager.system_uint16_expr; }
2575 | INT { $$ = TypeManager.system_int32_expr; }
2576 | UINT { $$ = TypeManager.system_uint32_expr; }
2577 | LONG { $$ = TypeManager.system_int64_expr; }
2578 | ULONG { $$ = TypeManager.system_uint64_expr; }
2579 | CHAR { $$ = TypeManager.system_char_expr; }
2580 | VOID { $$ = TypeManager.system_void_expr; }
2584 : type rank_specifiers
2586 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2591 // Expressions, section 7.5
2601 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2603 | parenthesized_expression
2604 | default_value_expression
2606 | invocation_expression
2610 | post_increment_expression
2611 | post_decrement_expression
2615 | checked_expression
2616 | unchecked_expression
2617 | pointer_member_access
2618 | anonymous_method_expression
2625 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2626 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2627 | NULL { $$ = NullLiteral.Null; }
2631 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2632 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2633 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2638 object v = lexer.Value;
2644 $$ = IntLiteral.Zero;
2646 $$ = IntLiteral.One;
2648 $$ = new IntLiteral (i);
2649 } else if (v is uint)
2650 $$ = new UIntLiteral ((UInt32) v);
2652 $$ = new LongLiteral ((Int64) v);
2653 else if (v is ulong)
2654 $$ = new ULongLiteral ((UInt64) v);
2656 Console.WriteLine ("OOPS. Unexpected result from scanner");
2661 : TRUE { $$ = new BoolLiteral (true); }
2662 | FALSE { $$ = new BoolLiteral (false); }
2665 parenthesized_expression_0
2666 : OPEN_PARENS expression CLOSE_PARENS
2669 lexer.Deambiguate_CloseParens ();
2670 // After this, the next token returned is one of
2671 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2672 // or CLOSE_PARENS_MINUS.
2676 parenthesized_expression
2677 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2681 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2683 // If a parenthesized expression is followed by a minus, we need to wrap
2684 // the expression inside a ParenthesizedExpression for the CS0075 check
2685 // in Binary.DoResolve().
2686 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2691 : primary_expression DOT IDENTIFIER opt_type_argument_list
2693 $$ = new MemberAccess ((Expression) $1, (string) $3,
2694 (TypeArguments) $4, lexer.Location);
2696 | predefined_type DOT IDENTIFIER opt_type_argument_list
2698 $$ = new MemberAccess ((Expression) $1, (string) $3,
2699 (TypeArguments) $4, lexer.Location);
2707 invocation_expression
2708 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2711 Location l = lexer.Location;
2712 Report.Error (1, l, "Parse error");
2714 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2716 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2718 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2720 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2722 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2727 : /* empty */ { $$ = null; }
2734 ArrayList list = new ArrayList (4);
2738 | argument_list COMMA argument
2740 ArrayList list = (ArrayList) $1;
2744 | argument_list error {
2745 CheckToken (1026, yyToken, ", or ) expected");
2752 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2754 | REF variable_reference
2756 $$ = new Argument ((Expression) $2, Argument.AType.Ref);
2758 | OUT variable_reference
2760 $$ = new Argument ((Expression) $2, Argument.AType.Out);
2762 | ARGLIST OPEN_PARENS argument_list CLOSE_PARENS
2764 ArrayList list = (ArrayList) $3;
2765 Argument[] args = new Argument [list.Count];
2766 list.CopyTo (args, 0);
2768 Expression expr = new Arglist (args, lexer.Location);
2769 $$ = new Argument (expr, Argument.AType.Expression);
2773 $$ = new Argument (new ArglistAccess (lexer.Location), Argument.AType.ArgList);
2778 : expression { note ("section 5.4"); $$ = $1; }
2782 : primary_expression OPEN_BRACKET expression_list CLOSE_BRACKET
2784 $$ = new ElementAccess ((Expression) $1, (ArrayList) $3, lexer.Location);
2786 | primary_expression rank_specifiers
2788 // So the super-trick is that primary_expression
2789 // can only be either a SimpleName or a MemberAccess.
2790 // The MemberAccess case arises when you have a fully qualified type-name like :
2792 // SimpleName is when you have
2795 Expression expr = (Expression) $1;
2796 if (expr is ComposedCast){
2797 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2798 } else if (!(expr is SimpleName || expr is MemberAccess || expr is ConstructedType)){
2799 Error_ExpectingTypeName (lexer.Location, expr);
2800 $$ = TypeManager.system_object_expr;
2803 // So we extract the string corresponding to the SimpleName
2806 $$ = new ComposedCast (expr, (string) $2, lexer.Location);
2814 ArrayList list = new ArrayList (4);
2818 | expression_list COMMA expression
2820 ArrayList list = (ArrayList) $1;
2829 $$ = new This (current_block, lexer.Location);
2834 : BASE DOT IDENTIFIER
2836 $$ = new BaseAccess ((string) $3, lexer.Location);
2838 | BASE OPEN_BRACKET expression_list CLOSE_BRACKET
2840 $$ = new BaseIndexerAccess ((ArrayList) $3, lexer.Location);
2843 Report.Error (175, lexer.Location, "Use of keyword `base' is not valid in this context");
2848 post_increment_expression
2849 : primary_expression OP_INC
2851 $$ = new UnaryMutator (UnaryMutator.Mode.PostIncrement,
2852 (Expression) $1, lexer.Location);
2856 post_decrement_expression
2857 : primary_expression OP_DEC
2859 $$ = new UnaryMutator (UnaryMutator.Mode.PostDecrement,
2860 (Expression) $1, lexer.Location);
2865 : object_or_delegate_creation_expression
2866 | array_creation_expression
2869 object_or_delegate_creation_expression
2870 : NEW type OPEN_PARENS opt_argument_list CLOSE_PARENS
2872 $$ = new New ((Expression) $2, (ArrayList) $4, lexer.Location);
2876 array_creation_expression
2877 : NEW type OPEN_BRACKET expression_list CLOSE_BRACKET
2879 opt_array_initializer
2881 $$ = new ArrayCreation ((Expression) $2, (ArrayList) $4, (string) $6, (ArrayList) $7, lexer.Location);
2883 | NEW type rank_specifiers array_initializer
2885 $$ = new ArrayCreation ((Expression) $2, (string) $3, (ArrayList) $4, lexer.Location);
2889 Report.Error (1031, lexer.Location, "Type expected");
2894 Report.Error (1526, lexer.Location, "new expression requires () or [] after type");
2910 : rank_specifier opt_rank_specifier
2912 $$ = (string) $2 + (string) $1;
2917 : OPEN_BRACKET opt_dim_separators CLOSE_BRACKET
2919 $$ = "[" + (string) $2 + "]";
2939 | dim_separators COMMA
2941 $$ = (string) $1 + ",";
2945 opt_array_initializer
2957 : OPEN_BRACE CLOSE_BRACE
2959 ArrayList list = new ArrayList (4);
2962 | OPEN_BRACE variable_initializer_list opt_comma CLOSE_BRACE
2964 $$ = (ArrayList) $2;
2968 variable_initializer_list
2969 : variable_initializer
2971 ArrayList list = new ArrayList (4);
2975 | variable_initializer_list COMMA variable_initializer
2977 ArrayList list = (ArrayList) $1;
2983 void_pointer_expression
2984 : void_pointer_expression STAR
2986 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2990 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
2995 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2997 $$ = new TypeOfVoid (lexer.Location);
2999 | TYPEOF OPEN_PARENS void_pointer_expression CLOSE_PARENS
3001 $$ = new TypeOf ((Expression) $3, lexer.Location);
3003 | TYPEOF OPEN_PARENS
3005 lexer.TypeOfParsing = true;
3009 lexer.TypeOfParsing = false;
3010 $$ = new TypeOf ((Expression) $4, lexer.Location);
3015 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
3016 $$ = new SizeOf ((Expression) $3, lexer.Location);
3021 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3023 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3027 unchecked_expression
3028 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3030 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3034 pointer_member_access
3035 : primary_expression OP_PTR IDENTIFIER
3039 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3040 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3044 anonymous_method_expression
3045 : DELEGATE opt_anonymous_method_signature {
3046 oob_stack.Push (current_local_parameters);
3047 current_local_parameters = (Parameters)$2;
3049 // Force the next block to be created as a ToplevelBlock
3050 oob_stack.Push (current_block);
3051 oob_stack.Push (top_current_block);
3052 oob_stack.Push (lexer.Location);
3053 current_block = null;
3055 Location loc = (Location) oob_stack.Pop ();
3056 top_current_block = (Block) oob_stack.Pop ();
3057 current_block = (Block) oob_stack.Pop ();
3058 if (RootContext.Version == LanguageVersion.ISO_1){
3059 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3062 ToplevelBlock anon_block = (ToplevelBlock) $4;
3064 anon_block.Parent = current_block;
3065 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3068 current_local_parameters = (Parameters) oob_stack.Pop ();
3072 opt_anonymous_method_signature
3073 : /* empty */ { $$ = null; }
3074 | anonymous_method_signature
3077 anonymous_method_signature
3078 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3081 $$ = Parameters.EmptyReadOnlyParameters;
3083 ArrayList par_list = (ArrayList) $2;
3084 Parameter [] pars = new Parameter [par_list.Count];
3085 par_list.CopyTo (pars);
3086 $$ = new Parameters (pars, null, lexer.Location);
3091 opt_anonymous_method_parameter_list
3092 : /* empty */ { $$ = null; }
3093 | anonymous_method_parameter_list { $$ = $1; }
3096 anonymous_method_parameter_list
3097 : anonymous_method_parameter
3099 ArrayList a = new ArrayList (4);
3103 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3105 ArrayList a = (ArrayList) $1;
3111 anonymous_method_parameter
3112 : opt_parameter_modifier type IDENTIFIER {
3113 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null);
3115 | PARAMS type IDENTIFIER {
3116 Report.Error (-221, lexer.Location, "params modifier not allowed in anonymous method declaration");
3121 default_value_expression
3122 : DEFAULT_OPEN_PARENS type CLOSE_PARENS
3124 $$ = new DefaultValueExpression ((Expression) $2, lexer.Location);
3129 : primary_expression
3130 | BANG prefixed_unary_expression
3132 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3134 | TILDE prefixed_unary_expression
3136 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3142 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3144 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3146 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3148 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3154 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3156 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3161 // The idea to split this out is from Rhys' grammar
3162 // to solve the problem with casts.
3164 prefixed_unary_expression
3166 | PLUS prefixed_unary_expression
3168 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3170 | MINUS prefixed_unary_expression
3172 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3174 | OP_INC prefixed_unary_expression
3176 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3177 (Expression) $2, lexer.Location);
3179 | OP_DEC prefixed_unary_expression
3181 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3182 (Expression) $2, lexer.Location);
3184 | STAR prefixed_unary_expression
3186 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3188 | BITWISE_AND prefixed_unary_expression
3190 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3194 pre_increment_expression
3195 : OP_INC prefixed_unary_expression
3197 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3198 (Expression) $2, lexer.Location);
3202 pre_decrement_expression
3203 : OP_DEC prefixed_unary_expression
3205 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3206 (Expression) $2, lexer.Location);
3210 multiplicative_expression
3211 : prefixed_unary_expression
3212 | multiplicative_expression STAR prefixed_unary_expression
3214 $$ = new Binary (Binary.Operator.Multiply,
3215 (Expression) $1, (Expression) $3, lexer.Location);
3217 | multiplicative_expression DIV prefixed_unary_expression
3219 $$ = new Binary (Binary.Operator.Division,
3220 (Expression) $1, (Expression) $3, lexer.Location);
3222 | multiplicative_expression PERCENT prefixed_unary_expression
3224 $$ = new Binary (Binary.Operator.Modulus,
3225 (Expression) $1, (Expression) $3, lexer.Location);
3230 : multiplicative_expression
3231 | additive_expression PLUS multiplicative_expression
3233 $$ = new Binary (Binary.Operator.Addition,
3234 (Expression) $1, (Expression) $3, lexer.Location);
3236 | additive_expression MINUS multiplicative_expression
3238 $$ = new Binary (Binary.Operator.Subtraction,
3239 (Expression) $1, (Expression) $3, lexer.Location);
3244 : additive_expression
3245 | shift_expression OP_SHIFT_LEFT additive_expression
3247 $$ = new Binary (Binary.Operator.LeftShift,
3248 (Expression) $1, (Expression) $3, lexer.Location);
3250 | shift_expression OP_SHIFT_RIGHT additive_expression
3252 $$ = new Binary (Binary.Operator.RightShift,
3253 (Expression) $1, (Expression) $3, lexer.Location);
3257 relational_expression
3259 | relational_expression OP_LT shift_expression
3261 $$ = new Binary (Binary.Operator.LessThan,
3262 (Expression) $1, (Expression) $3, lexer.Location);
3264 | relational_expression OP_GT shift_expression
3266 $$ = new Binary (Binary.Operator.GreaterThan,
3267 (Expression) $1, (Expression) $3, lexer.Location);
3269 | relational_expression OP_LE shift_expression
3271 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3272 (Expression) $1, (Expression) $3, lexer.Location);
3274 | relational_expression OP_GE shift_expression
3276 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3277 (Expression) $1, (Expression) $3, lexer.Location);
3279 | relational_expression IS type
3281 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3283 | relational_expression AS type
3285 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3290 : relational_expression
3291 | equality_expression OP_EQ relational_expression
3293 $$ = new Binary (Binary.Operator.Equality,
3294 (Expression) $1, (Expression) $3, lexer.Location);
3296 | equality_expression OP_NE relational_expression
3298 $$ = new Binary (Binary.Operator.Inequality,
3299 (Expression) $1, (Expression) $3, lexer.Location);
3304 : equality_expression
3305 | and_expression BITWISE_AND equality_expression
3307 $$ = new Binary (Binary.Operator.BitwiseAnd,
3308 (Expression) $1, (Expression) $3, lexer.Location);
3312 exclusive_or_expression
3314 | exclusive_or_expression CARRET and_expression
3316 $$ = new Binary (Binary.Operator.ExclusiveOr,
3317 (Expression) $1, (Expression) $3, lexer.Location);
3321 inclusive_or_expression
3322 : exclusive_or_expression
3323 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3325 $$ = new Binary (Binary.Operator.BitwiseOr,
3326 (Expression) $1, (Expression) $3, lexer.Location);
3330 conditional_and_expression
3331 : inclusive_or_expression
3332 | conditional_and_expression OP_AND inclusive_or_expression
3334 $$ = new Binary (Binary.Operator.LogicalAnd,
3335 (Expression) $1, (Expression) $3, lexer.Location);
3339 conditional_or_expression
3340 : conditional_and_expression
3341 | conditional_or_expression OP_OR conditional_and_expression
3343 $$ = new Binary (Binary.Operator.LogicalOr,
3344 (Expression) $1, (Expression) $3, lexer.Location);
3348 conditional_expression
3349 : conditional_or_expression
3350 | conditional_or_expression INTERR expression COLON expression
3352 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3356 assignment_expression
3357 : prefixed_unary_expression ASSIGN expression
3359 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3361 | prefixed_unary_expression OP_MULT_ASSIGN expression
3363 Location l = lexer.Location;
3365 $$ = new CompoundAssign (
3366 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3368 | prefixed_unary_expression OP_DIV_ASSIGN expression
3370 Location l = lexer.Location;
3372 $$ = new CompoundAssign (
3373 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3375 | prefixed_unary_expression OP_MOD_ASSIGN expression
3377 Location l = lexer.Location;
3379 $$ = new CompoundAssign (
3380 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3382 | prefixed_unary_expression OP_ADD_ASSIGN expression
3384 Location l = lexer.Location;
3386 $$ = new CompoundAssign (
3387 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3389 | prefixed_unary_expression OP_SUB_ASSIGN expression
3391 Location l = lexer.Location;
3393 $$ = new CompoundAssign (
3394 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3396 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3398 Location l = lexer.Location;
3400 $$ = new CompoundAssign (
3401 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3403 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3405 Location l = lexer.Location;
3407 $$ = new CompoundAssign (
3408 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3410 | prefixed_unary_expression OP_AND_ASSIGN expression
3412 Location l = lexer.Location;
3414 $$ = new CompoundAssign (
3415 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3417 | prefixed_unary_expression OP_OR_ASSIGN expression
3419 Location l = lexer.Location;
3421 $$ = new CompoundAssign (
3422 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3424 | prefixed_unary_expression OP_XOR_ASSIGN expression
3426 Location l = lexer.Location;
3428 $$ = new CompoundAssign (
3429 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3434 : conditional_expression
3435 | assignment_expression
3455 MemberName name = MakeName ((MemberName) $5);
3456 bool partial = (bool) $3;
3457 int mod_flags = (int) $2;
3460 ClassPart part = PartialContainer.CreatePart (
3461 current_namespace, current_container, name, mod_flags,
3462 (Attributes) $1, Kind.Class, lexer.Location);
3464 current_container = part.PartialContainer;
3465 current_class = part;
3467 if ((mod_flags & Modifiers.STATIC) != 0) {
3468 current_class = new StaticClass (
3469 current_namespace, current_container, name,
3470 mod_flags, (Attributes) $1, lexer.Location);
3472 current_class = new Class (
3473 current_namespace, current_container, name,
3474 mod_flags, (Attributes) $1, lexer.Location);
3477 current_container = current_class;
3478 RootContext.Tree.RecordDecl (name.GetName (true), current_class);
3481 lexer.ConstraintsParsing = true;
3484 opt_type_parameter_constraints_clauses
3486 lexer.ConstraintsParsing = false;
3489 if (current_class.Name == "System.Object") {
3490 Report.Error (537, current_class.Location,
3491 "The class System.Object cannot have a base " +
3492 "class or implement an interface.");
3494 current_class.Bases = (ArrayList) $7;
3497 current_class.SetParameterInfo ((ArrayList) $8);
3499 if (RootContext.Documentation != null) {
3500 current_class.DocComment = Lexer.consume_doc_comment ();
3501 Lexer.doc_state = XmlCommentState.Allowed;
3504 current_class.Register ();
3508 if (RootContext.Documentation != null)
3509 Lexer.doc_state = XmlCommentState.Allowed;
3515 current_container = current_container.Parent;
3516 current_class = current_container;
3522 { $$ = (bool) false; }
3524 { $$ = (bool) true; }
3528 : /* empty */ { $$ = (int) 0; }
3534 | modifiers modifier
3539 if ((m1 & m2) != 0) {
3540 Location l = lexer.Location;
3541 Report.Error (1004, l, "Duplicate modifier: `" + Modifiers.Name (m2) + "'");
3543 $$ = (int) (m1 | m2);
3548 : NEW { $$ = Modifiers.NEW; }
3549 | PUBLIC { $$ = Modifiers.PUBLIC; }
3550 | PROTECTED { $$ = Modifiers.PROTECTED; }
3551 | INTERNAL { $$ = Modifiers.INTERNAL; }
3552 | PRIVATE { $$ = Modifiers.PRIVATE; }
3553 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3554 | SEALED { $$ = Modifiers.SEALED; }
3555 | STATIC { $$ = Modifiers.STATIC; }
3556 | READONLY { $$ = Modifiers.READONLY; }
3557 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3558 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3559 | EXTERN { $$ = Modifiers.EXTERN; }
3560 | VOLATILE { $$ = Modifiers.VOLATILE; }
3561 | UNSAFE { $$ = Modifiers.UNSAFE; }
3565 : /* empty */ { $$ = null; }
3566 | class_base { $$ = $1; }
3570 : COLON type_list { $$ = $2; }
3573 opt_type_parameter_constraints_clauses
3574 : /* empty */ { $$ = null; }
3575 | type_parameter_constraints_clauses
3579 type_parameter_constraints_clauses
3580 : type_parameter_constraints_clause {
3581 ArrayList constraints = new ArrayList (1);
3582 constraints.Add ($1);
3585 | type_parameter_constraints_clauses type_parameter_constraints_clause {
3586 ArrayList constraints = (ArrayList) $1;
3588 constraints.Add ($2);
3593 type_parameter_constraints_clause
3594 : WHERE IDENTIFIER COLON type_parameter_constraints {
3595 $$ = new Constraints ((string) $2, (ArrayList) $4, lexer.Location);
3599 type_parameter_constraints
3600 : type_parameter_constraint {
3601 ArrayList constraints = new ArrayList (1);
3602 constraints.Add ($1);
3605 | type_parameter_constraints COMMA type_parameter_constraint {
3606 ArrayList constraints = (ArrayList) $1;
3608 constraints.Add ($3);
3613 type_parameter_constraint
3615 | NEW OPEN_PARENS CLOSE_PARENS {
3616 $$ = SpecialConstraint.Constructor;
3619 $$ = SpecialConstraint.ReferenceType;
3622 $$ = SpecialConstraint.ValueType;
3631 // A block is "contained" on the following places:
3633 // property_declaration as part of the accessor body (get/set)
3634 // operator_declaration
3635 // constructor_declaration
3636 // destructor_declaration
3637 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3642 if (current_block == null){
3643 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3644 top_current_block = current_block;
3646 current_block = new Block (current_block, current_local_parameters,
3647 lexer.Location, Location.Null);
3650 opt_statement_list CLOSE_BRACE
3652 while (current_block.Implicit)
3653 current_block = current_block.Parent;
3655 current_block.SetEndLocation (lexer.Location);
3656 current_block = current_block.Parent;
3657 if (current_block == null)
3658 top_current_block = null;
3669 | statement_list statement
3673 : declaration_statement
3675 if ($1 != null && (Block) $1 != current_block){
3676 current_block.AddStatement ((Statement) $1);
3677 current_block = (Block) $1;
3680 | valid_declaration_statement
3682 current_block.AddStatement ((Statement) $1);
3687 valid_declaration_statement
3690 | expression_statement
3691 | selection_statement
3692 | iteration_statement
3696 | unchecked_statement
3704 : valid_declaration_statement
3705 | declaration_statement
3707 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration.");
3712 Report.Error (1023, lexer.Location, "An embedded statement may not be a labeled statement.");
3720 $$ = EmptyStatement.Value;
3727 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3729 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3730 current_block.AddStatement (labeled);
3735 declaration_statement
3736 : local_variable_declaration SEMICOLON
3739 DictionaryEntry de = (DictionaryEntry) $1;
3741 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3745 | local_constant_declaration SEMICOLON
3748 DictionaryEntry de = (DictionaryEntry) $1;
3750 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3756 * The following is from Rhys' grammar:
3757 * > Types in local variable declarations must be recognized as
3758 * > expressions to prevent reduce/reduce errors in the grammar.
3759 * > The expressions are converted into types during semantic analysis.
3762 : primary_expression opt_rank_specifier
3764 // FIXME: Do something smart here regarding the composition of the type.
3766 // Ok, the above "primary_expression" is there to get rid of
3767 // both reduce/reduce and shift/reduces in the grammar, it should
3768 // really just be "type_name". If you use type_name, a reduce/reduce
3769 // creeps up. If you use namespace_or_type_name (which is all we need
3770 // really) two shift/reduces appear.
3773 // So the super-trick is that primary_expression
3774 // can only be either a SimpleName or a MemberAccess.
3775 // The MemberAccess case arises when you have a fully qualified type-name like :
3777 // SimpleName is when you have
3780 Expression expr = (Expression) $1;
3781 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3782 Error_ExpectingTypeName (lexer.Location, expr);
3786 // So we extract the string corresponding to the SimpleName
3790 if ((string) $2 == "")
3793 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3796 | builtin_types opt_rank_specifier
3798 if ((string) $2 == "")
3801 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3805 local_variable_pointer_type
3806 : primary_expression STAR
3808 Expression expr = (Expression) $1;
3809 Location l = lexer.Location;
3811 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast || expr is ConstructedType)) {
3812 Error_ExpectingTypeName (l, expr);
3816 $$ = new ComposedCast ((Expression) $1, "*", l);
3818 | builtin_types STAR
3820 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3824 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3826 | local_variable_pointer_type STAR
3828 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3832 local_variable_declaration
3833 : local_variable_type variable_declarators
3836 $$ = new DictionaryEntry ($1, $2);
3840 | local_variable_pointer_type opt_rank_specifier variable_declarators
3845 if ((string) $2 == "")
3846 t = (Expression) $1;
3848 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3849 $$ = new DictionaryEntry (t, $3);
3855 local_constant_declaration
3856 : CONST local_variable_type constant_declarators
3859 $$ = new DictionaryEntry ($2, $3);
3865 expression_statement
3866 : statement_expression SEMICOLON
3873 // We have to do the wrapping here and not in the case above,
3874 // because statement_expression is used for example in for_statement
3876 statement_expression
3877 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3878 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3879 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3880 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3881 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3882 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3883 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3885 Report.Error (1002, lexer.Location, "Expecting `;'");
3890 object_creation_expression
3891 : object_or_delegate_creation_expression
3892 { note ("complain if this is a delegate maybe?"); }
3901 : if_statement_open if_statement_rest
3910 oob_stack.Push (lexer.Location);
3915 : boolean_expression CLOSE_PARENS
3918 Location l = (Location) oob_stack.Pop ();
3920 $$ = new If ((Expression) $1, (Statement) $3, l);
3922 if (RootContext.WarningLevel >= 4){
3923 if ($3 == EmptyStatement.Value)
3924 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3928 | boolean_expression CLOSE_PARENS
3929 embedded_statement ELSE embedded_statement
3931 Location l = (Location) oob_stack.Pop ();
3933 $$ = new If ((Expression) $1, (Statement) $3, (Statement) $5, l);
3938 : SWITCH OPEN_PARENS
3940 oob_stack.Push (lexer.Location);
3941 switch_stack.Push (current_block);
3943 expression CLOSE_PARENS
3946 $$ = new Switch ((Expression) $4, (ArrayList) $6, (Location) oob_stack.Pop ());
3947 current_block = (Block) switch_stack.Pop ();
3963 Report.Error (1522, lexer.Location, "Empty switch block");
3971 ArrayList sections = new ArrayList (4);
3976 | switch_sections switch_section
3978 ArrayList sections = (ArrayList) $1;
3988 current_block = current_block.CreateSwitchBlock (lexer.Location);
3992 Block topmost = current_block;
3994 while (topmost.Implicit)
3995 topmost = topmost.Parent;
3996 $$ = new SwitchSection ((ArrayList) $1, topmost);
4003 ArrayList labels = new ArrayList (4);
4008 | switch_labels switch_label
4010 ArrayList labels = (ArrayList) ($1);
4018 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
4019 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
4022 1523, lexer.Location,
4023 "The keyword case or default must precede code in switch block");
4037 oob_stack.Push (lexer.Location);
4039 boolean_expression CLOSE_PARENS embedded_statement
4041 Location l = (Location) oob_stack.Pop ();
4042 $$ = new While ((Expression) $4, (Statement) $6, l);
4044 if (RootContext.WarningLevel >= 4){
4045 if ($6 == EmptyStatement.Value)
4046 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4052 : DO embedded_statement
4054 oob_stack.Push (lexer.Location);
4056 boolean_expression CLOSE_PARENS SEMICOLON
4058 Location l = (Location) oob_stack.Pop ();
4060 $$ = new Do ((Statement) $2, (Expression) $6, l);
4066 opt_for_initializer SEMICOLON
4068 Block assign_block = new Block (current_block);
4069 current_block = assign_block;
4071 if ($3 is DictionaryEntry){
4072 DictionaryEntry de = (DictionaryEntry) $3;
4074 Expression type = (Expression) de.Key;
4075 ArrayList var_declarators = (ArrayList) de.Value;
4077 foreach (VariableDeclaration decl in var_declarators){
4081 vi = current_block.AddVariable (
4082 type, decl.identifier, current_local_parameters, decl.Location);
4086 Location l = lexer.Location;
4088 if (decl.expression_or_array_initializer is Expression){
4089 expr = (Expression) decl.expression_or_array_initializer;
4090 } else if (decl.expression_or_array_initializer == null) {
4093 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4094 expr = new ArrayCreation (type, "", init, decl.Location);
4097 LocalVariableReference var;
4098 var = new LocalVariableReference (assign_block, decl.identifier, l);
4101 Assign a = new Assign (var, expr, decl.Location);
4103 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4109 oob_stack.Push (lexer.Location);
4111 opt_for_condition SEMICOLON
4112 opt_for_iterator CLOSE_PARENS
4115 Location l = (Location) oob_stack.Pop ();
4117 For f = new For ((Statement) $3, (Expression) $6, (Statement) $8, (Statement) $10, l);
4119 if (RootContext.WarningLevel >= 4){
4120 if ($10 == EmptyStatement.Value)
4121 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4124 current_block.AddStatement (f);
4125 while (current_block.Implicit)
4126 current_block = current_block.Parent;
4128 current_block = current_block.Parent;
4133 : /* empty */ { $$ = EmptyStatement.Value; }
4138 : local_variable_declaration
4139 | statement_expression_list
4143 : /* empty */ { $$ = null; }
4144 | boolean_expression
4148 : /* empty */ { $$ = EmptyStatement.Value; }
4153 : statement_expression_list
4156 statement_expression_list
4157 : statement_expression
4159 // CHANGE: was `null'
4160 Block b = new Block (current_block, Block.Flags.Implicit);
4162 b.AddStatement ((Statement) $1);
4165 | statement_expression_list COMMA statement_expression
4167 Block b = (Block) $1;
4169 b.AddStatement ((Statement) $3);
4175 : FOREACH OPEN_PARENS type IDENTIFIER IN
4177 oob_stack.Push (lexer.Location);
4179 expression CLOSE_PARENS
4181 oob_stack.Push (current_block);
4183 Block foreach_block = new Block (current_block);
4184 LocalVariableReference v = null;
4185 Location l = lexer.Location;
4188 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
4192 // Get a writable reference to this read-only variable.
4193 v = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4195 current_block = foreach_block;
4198 oob_stack.Push (current_block);
4202 Block foreach_block = (Block) oob_stack.Pop ();
4203 LocalVariableReference v = (LocalVariableReference) oob_stack.Pop ();
4204 Block prev_block = (Block) oob_stack.Pop ();
4205 Location l = (Location) oob_stack.Pop ();
4207 current_block = prev_block;
4210 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4211 foreach_block.AddStatement (f);
4220 | continue_statement
4230 $$ = new Break (lexer.Location);
4235 : CONTINUE SEMICOLON
4237 $$ = new Continue (lexer.Location);
4242 : GOTO IDENTIFIER SEMICOLON
4244 $$ = new Goto (current_block, (string) $2, lexer.Location);
4246 | GOTO CASE constant_expression SEMICOLON
4248 $$ = new GotoCase ((Expression) $3, lexer.Location);
4250 | GOTO DEFAULT SEMICOLON
4252 $$ = new GotoDefault (lexer.Location);
4257 : RETURN opt_expression SEMICOLON
4259 $$ = new Return ((Expression) $2, lexer.Location);
4264 : THROW opt_expression SEMICOLON
4266 $$ = new Throw ((Expression) $2, lexer.Location);
4271 : IDENTIFIER RETURN expression SEMICOLON
4273 string s = (string) $1;
4275 Report.Error (1003, lexer.Location, "; expected");
4278 if (RootContext.Version == LanguageVersion.ISO_1){
4279 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4282 if (iterator_container == null){
4283 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4286 iterator_container.SetYields ();
4287 $$ = new Yield ((Expression) $3, lexer.Location);
4290 | IDENTIFIER BREAK SEMICOLON
4292 string s = (string) $1;
4294 Report.Error (1003, lexer.Location, "; expected");
4297 if (RootContext.Version == LanguageVersion.ISO_1){
4298 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4301 if (iterator_container == null){
4302 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4305 iterator_container.SetYields ();
4306 $$ = new YieldBreak (lexer.Location);
4317 : TRY block catch_clauses
4321 ArrayList c = (ArrayList)$3;
4322 for (int i = 0; i < c.Count; ++i) {
4323 Catch cc = (Catch) c [i];
4325 if (i != c.Count - 1)
4326 Report.Error (1017, cc.loc, "Empty catch block must be the last in a series of catch blocks");
4333 // Now s contains the list of specific catch clauses
4334 // and g contains the general one.
4336 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4338 | TRY block opt_catch_clauses FINALLY block
4341 ArrayList s = new ArrayList (4);
4342 ArrayList catch_list = (ArrayList) $3;
4344 if (catch_list != null){
4345 foreach (Catch cc in catch_list) {
4353 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4357 Report.Error (1524, lexer.Location, "Expected catch or finally");
4362 : /* empty */ { $$ = null; }
4369 ArrayList l = new ArrayList (4);
4374 | catch_clauses catch_clause
4376 ArrayList l = (ArrayList) $1;
4384 : /* empty */ { $$ = null; }
4389 : CATCH opt_catch_args
4391 Expression type = null;
4395 DictionaryEntry cc = (DictionaryEntry) $2;
4396 type = (Expression) cc.Key;
4397 id = (string) cc.Value;
4400 ArrayList one = new ArrayList (4);
4401 Location loc = lexer.Location;
4403 one.Add (new VariableDeclaration (id, null, loc));
4406 current_block = new Block (current_block);
4407 Block b = declare_local_variables (type, one, loc);
4412 Expression type = null;
4416 DictionaryEntry cc = (DictionaryEntry) $2;
4417 type = (Expression) cc.Key;
4418 id = (string) cc.Value;
4422 // FIXME: I can change this for an assignment.
4424 while (current_block != (Block) $1)
4425 current_block = current_block.Parent;
4430 $$ = new Catch (type, id , (Block) $4, ((Block) $4).loc);
4435 : /* empty */ { $$ = null; }
4440 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4442 $$ = new DictionaryEntry ($2, $3);
4449 $$ = new Checked ((Block) $2);
4456 $$ = new Unchecked ((Block) $2);
4463 if (!RootContext.Unsafe){
4464 Report.Error (227, lexer.Location,
4465 "Unsafe code can only be used if --unsafe is used");
4468 $$ = new Unsafe ((Block) $3);
4474 type fixed_pointer_declarators
4477 ArrayList list = (ArrayList) $4;
4478 Expression type = (Expression) $3;
4479 Location l = lexer.Location;
4480 int top = list.Count;
4482 Block assign_block = new Block (current_block);
4483 current_block = assign_block;
4485 for (int i = 0; i < top; i++){
4486 Pair p = (Pair) list [i];
4489 v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
4503 Location l = (Location) oob_stack.Pop ();
4505 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $7, l);
4507 if (RootContext.WarningLevel >= 4){
4508 if ($7 == EmptyStatement.Value)
4509 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4512 current_block.AddStatement (f);
4513 while (current_block.Implicit)
4514 current_block = current_block.Parent;
4516 current_block = current_block.Parent;
4520 fixed_pointer_declarators
4521 : fixed_pointer_declarator {
4522 ArrayList declarators = new ArrayList (4);
4524 declarators.Add ($1);
4527 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4529 ArrayList declarators = (ArrayList) $1;
4531 declarators.Add ($3);
4536 fixed_pointer_declarator
4537 : IDENTIFIER ASSIGN expression
4539 $$ = new Pair ($1, $3);
4543 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4549 : LOCK OPEN_PARENS expression CLOSE_PARENS
4555 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4560 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS
4562 Block assign_block = new Block (current_block);
4563 current_block = assign_block;
4565 oob_stack.Push (lexer.Location);
4567 if ($3 is DictionaryEntry){
4568 DictionaryEntry de = (DictionaryEntry) $3;
4569 Location l = lexer.Location;
4571 Expression type = (Expression) de.Key;
4572 ArrayList var_declarators = (ArrayList) de.Value;
4574 ArrayList vars = new ArrayList (4);
4576 foreach (VariableDeclaration decl in var_declarators){
4578 LocalInfo vi = current_block.AddVariable (
4579 type, decl.identifier,
4580 current_local_parameters, decl.Location);
4586 if (decl.expression_or_array_initializer is Expression){
4587 expr = (Expression) decl.expression_or_array_initializer;
4589 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4591 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4594 expr = new ArrayCreation (type, "", init, decl.Location);
4597 LocalVariableReference var;
4599 // Get a writable reference to this read-only variable.
4600 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4602 // This is so that it is not a warning on using variables
4605 vars.Add (new DictionaryEntry (var, expr));
4607 // Assign a = new Assign (var, expr, decl.Location);
4608 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4610 $3 = new DictionaryEntry (type, vars);
4615 Using u = new Using ($3, (Statement) $6, (Location) oob_stack.Pop ());
4616 current_block.AddStatement (u);
4617 while (current_block.Implicit)
4618 current_block = current_block.Parent;
4620 current_block = current_block.Parent;
4624 resource_acquisition
4625 : local_variable_declaration
4632 // A class used to pass around variable declarations and constants
4634 public class VariableDeclaration {
4635 public string identifier;
4636 public object expression_or_array_initializer;
4637 public Location Location;
4638 public Attributes OptAttributes;
4639 public string DocComment;
4641 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4643 this.identifier = id;
4644 this.expression_or_array_initializer = eoai;
4646 this.OptAttributes = opt_attrs;
4649 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4655 /// Used to pass around interface property information
4657 public class InterfaceAccessorInfo {
4659 public readonly Accessor Get, Set;
4661 public InterfaceAccessorInfo (bool has_get, bool has_set,
4662 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4665 Report.Error (275, get_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4667 Report.Error (275, set_loc, "Accessibility modifiers can not be used on accessors in interfaces");
4670 Get = new Accessor (null, 0, get_attrs, get_loc);
4672 Set = new Accessor (null, 0, set_attrs, set_loc);
4679 // A class used to hold info about an indexer declarator
4681 public class IndexerDeclaration {
4682 public Expression type;
4683 public MemberName interface_type;
4684 public Parameters param_list;
4686 public IndexerDeclaration (Expression type, MemberName interface_type,
4687 Parameters param_list)
4690 this.interface_type = interface_type;
4691 this.param_list = param_list;
4696 // We use this when we do not have an object in advance that is an IIteratorContainer
4698 public class SimpleIteratorContainer : IIteratorContainer {
4701 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4706 public static SimpleIteratorContainer GetSimple () {
4707 Simple.Yields = false;
4711 public void SetYields () { Yields = true; }
4715 // A class used to hold info about an operator declarator
4717 public class OperatorDeclaration {
4718 public Operator.OpType optype;
4719 public Expression ret_type, arg1type, arg2type;
4720 public string arg1name, arg2name;
4721 public Location location;
4723 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4724 Expression arg1type, string arg1name,
4725 Expression arg2type, string arg2name, Location location)
4728 this.ret_type = ret_type;
4729 this.arg1type = arg1type;
4730 this.arg1name = arg1name;
4731 this.arg2type = arg2type;
4732 this.arg2name = arg2name;
4733 this.location = location;
4738 void Error_ExpectingTypeName (Location l, Expression expr)
4740 if (expr is Invocation){
4741 Report.Error (1002, l, "; expected");
4743 Report.Error (-1, l, "Invalid Type definition");
4748 // Given the @class_name name, it creates a fully qualified name
4749 // based on the containing declaration space
4752 MakeName (MemberName class_name)
4754 string ns = current_namespace.FullName;
4756 if (current_container.Name == ""){
4758 return new MemberName (new MemberName (ns), class_name);
4762 return new MemberName (current_container.MemberName, class_name);
4766 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4768 Block implicit_block;
4769 ArrayList inits = null;
4772 // We use the `Used' property to check whether statements
4773 // have been added to the current block. If so, we need
4774 // to create another block to contain the new declaration
4775 // otherwise, as an optimization, we use the same block to
4776 // add the declaration.
4778 // FIXME: A further optimization is to check if the statements
4779 // that were added were added as part of the initialization
4780 // below. In which case, no other statements have been executed
4781 // and we might be able to reduce the number of blocks for
4782 // situations like this:
4784 // int j = 1; int k = j + 1;
4786 if (current_block.Used)
4787 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4789 implicit_block = current_block;
4791 foreach (VariableDeclaration decl in variable_declarators){
4793 if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
4794 if (decl.expression_or_array_initializer != null){
4796 inits = new ArrayList (4);
4803 return implicit_block;
4805 foreach (VariableDeclaration decl in inits){
4809 if (decl.expression_or_array_initializer is Expression){
4810 expr = (Expression) decl.expression_or_array_initializer;
4813 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4815 expr = new ArrayCreation (type, "", init, decl.Location);
4818 LocalVariableReference var;
4819 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4821 assign = new Assign (var, expr, decl.Location);
4823 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4826 return implicit_block;
4829 Block declare_local_constants (Expression type, ArrayList declarators)
4831 Block implicit_block;
4833 if (current_block.Used)
4834 implicit_block = new Block (current_block, Block.Flags.Implicit);
4836 implicit_block = current_block;
4838 foreach (VariableDeclaration decl in declarators){
4839 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
4840 current_local_parameters, decl.Location);
4843 return implicit_block;
4846 void CheckAttributeTarget (string a)
4850 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4854 Location l = lexer.Location;
4855 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4861 void CheckUnaryOperator (Operator.OpType op)
4865 case Operator.OpType.LogicalNot:
4866 case Operator.OpType.OnesComplement:
4867 case Operator.OpType.Increment:
4868 case Operator.OpType.Decrement:
4869 case Operator.OpType.True:
4870 case Operator.OpType.False:
4871 case Operator.OpType.Addition:
4872 case Operator.OpType.Subtraction:
4877 Location l = lexer.Location;
4878 Report.Error (1019, l, "Overloadable unary operator expected");
4884 void CheckBinaryOperator (Operator.OpType op)
4888 case Operator.OpType.Addition:
4889 case Operator.OpType.Subtraction:
4890 case Operator.OpType.Multiply:
4891 case Operator.OpType.Division:
4892 case Operator.OpType.Modulus:
4893 case Operator.OpType.BitwiseAnd:
4894 case Operator.OpType.BitwiseOr:
4895 case Operator.OpType.ExclusiveOr:
4896 case Operator.OpType.LeftShift:
4897 case Operator.OpType.RightShift:
4898 case Operator.OpType.Equality:
4899 case Operator.OpType.Inequality:
4900 case Operator.OpType.GreaterThan:
4901 case Operator.OpType.LessThan:
4902 case Operator.OpType.GreaterThanOrEqual:
4903 case Operator.OpType.LessThanOrEqual:
4907 Location l = lexer.Location;
4908 Report.Error (1020, l, "Overloadable binary operator expected");
4914 void syntax_error (Location l, string msg)
4916 Report.Error (1003, l, "Syntax error, " + msg);
4919 void output (string s)
4921 Console.WriteLine (s);
4924 void note (string s)
4926 // Used to put annotations
4931 public Tokenizer Lexer {
4937 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4939 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4940 this.name = file.Name;
4942 current_container = RootContext.Tree.Types;
4943 current_container.NamespaceEntry = current_namespace;
4944 oob_stack = new Stack ();
4945 switch_stack = new Stack ();
4947 lexer = new Tokenizer (reader, file, defines);
4950 public void parse ()
4953 if (yacc_verbose_flag > 1)
4954 yyparse (lexer, new yydebug.yyDebugSimple ());
4957 Tokenizer tokenizer = lexer as Tokenizer;
4958 tokenizer.cleanup ();
4959 } catch (Exception e){
4961 // Removed for production use, use parser verbose to get the output.
4963 // Console.WriteLine (e);
4964 Report.Error (-25, lexer.Location, "Parsing error");
4965 if (yacc_verbose_flag > 0)
4966 Console.WriteLine (e);
4970 void CheckToken (int error, int yyToken, string msg)
4972 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4973 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4976 Report.Error (error, lexer.Location, msg);
4979 void CheckIdentifierToken (int yyToken)
4981 CheckToken (1041, yyToken, "Identifier expected");
4984 string ConsumeStoredComment ()
4986 string s = tmpComment;
4988 Lexer.doc_state = XmlCommentState.Allowed;