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;
47 /// This is used by the unary_expression code to resolve
48 /// a name against a parameter.
50 Parameters current_local_parameters;
53 /// Using during property parsing to describe the implicit
54 /// value parameter that is passed to the "set" and "get"accesor
55 /// methods (properties and indexers).
57 Expression implicit_value_parameter_type;
58 Parameters indexer_parameters;
61 /// Used to determine if we are parsing the get/set pair
62 /// of an indexer or a property
67 /// An out-of-band stack.
76 static public int yacc_verbose_flag;
78 // Name of the file we are parsing
87 /// Temporary Xml documentation cache.
88 /// For enum types, we need one more temporary store.
91 string enumTypeComment;
93 /// Current attribute target
94 string current_attr_target;
96 /// assembly and module attribute definitions are enabled
97 bool global_attrs_enabled = true;
99 bool has_get, has_set;
104 %token NONE /* This token is never returned by our lexer */
105 %token ERROR // This is used not by the parser, but by the tokenizer.
109 *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 ")"
220 %token BITWISE_AND "&"
221 %token BITWISE_OR "|"
228 /* C# multi-character operators. */
231 %token OP_SHIFT_LEFT "<<"
232 %token OP_SHIFT_RIGHT ">>"
239 %token OP_MULT_ASSIGN "*="
240 %token OP_DIV_ASSIGN "/="
241 %token OP_MOD_ASSIGN "%="
242 %token OP_ADD_ASSIGN "+="
243 %token OP_SUB_ASSIGN "-="
244 %token OP_SHIFT_LEFT_ASSIGN "<<="
245 %token OP_SHIFT_RIGHT_ASSIGN ">>="
246 %token OP_AND_ASSIGN "&="
247 %token OP_XOR_ASSIGN "^="
248 %token OP_OR_ASSIGN "|="
252 %token LITERAL_INTEGER "int literal"
253 %token LITERAL_FLOAT "float literal"
254 %token LITERAL_DOUBLE "double literal"
255 %token LITERAL_DECIMAL "decimal literal"
256 %token LITERAL_CHARACTER "character literal"
257 %token LITERAL_STRING "string literal"
260 %token CLOSE_PARENS_CAST
261 %token CLOSE_PARENS_NO_CAST
262 %token CLOSE_PARENS_OPEN_PARENS
263 %token CLOSE_PARENS_MINUS
265 /* Add precedence rules to solve dangling else s/r conflict */
274 %left OP_SHIFT_LEFT OP_SHIFT_RIGHT
276 %left STAR DIV PERCENT
277 %right BANG CARRET UMINUS
278 %nonassoc OP_INC OP_DEC
280 %left OPEN_BRACKET OPEN_BRACE
284 %start compilation_unit
288 : outer_declarations opt_EOF
289 | outer_declarations global_attributes opt_EOF
290 | global_attributes opt_EOF
291 | opt_EOF /* allow empty files */
297 Lexer.check_incorrect_doc_comment ();
301 Lexer.check_incorrect_doc_comment ();
307 | outer_declarations outer_declaration
312 | namespace_member_declaration
317 | using_directives using_directive
321 : using_alias_directive
323 if (RootContext.Documentation != null)
324 Lexer.doc_state = XmlCommentState.Allowed;
326 | using_namespace_directive
328 if (RootContext.Documentation != null)
329 Lexer.doc_state = XmlCommentState.Allowed;
333 using_alias_directive
334 : USING IDENTIFIER ASSIGN
335 namespace_or_type_name SEMICOLON
337 current_namespace.UsingAlias ((string) $2, (MemberName) $4, lexer.Location);
340 CheckIdentifierToken (yyToken);
344 using_namespace_directive
345 : USING namespace_name SEMICOLON
347 current_namespace.Using ((MemberName) $2, lexer.Location);
352 // Strictly speaking, namespaces don't have attributes but
353 // we parse global attributes along with namespace declarations and then
356 namespace_declaration
357 : opt_attributes NAMESPACE namespace_or_type_name
360 Report.Error(1671, Lexer.Location, "A namespace declaration cannot have modifiers or attributes");
363 MemberName name = (MemberName) $3;
365 current_namespace = new NamespaceEntry (
366 current_namespace, file, name.GetName (), lexer.Location);
368 namespace_body opt_semicolon
370 current_namespace = current_namespace.Parent;
385 : namespace_or_type_name
391 if (RootContext.Documentation != null)
392 Lexer.doc_state = XmlCommentState.Allowed;
395 opt_namespace_member_declarations
404 opt_namespace_member_declarations
406 | namespace_member_declarations
409 namespace_member_declarations
410 : namespace_member_declaration
411 | namespace_member_declarations namespace_member_declaration
414 namespace_member_declaration
418 DeclSpace ds = (DeclSpace)$1;
420 if ((ds.ModFlags & (Modifiers.PRIVATE|Modifiers.PROTECTED)) != 0){
421 Report.Error (1527, lexer.Location,
422 "Namespace elements cannot be explicitly declared as private, protected or protected internal");
425 current_namespace.DeclarationFound = true;
427 | namespace_declaration {
428 current_namespace.DeclarationFound = true;
431 | field_declaration {
432 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
434 | method_declaration {
435 Report.Error (116, lexer.Location, "A namespace can only contain types and namespace declarations");
442 | interface_declaration
444 | delegate_declaration
446 // Enable this when we have handled all errors, because this acts as a generic fallback
449 // Console.WriteLine ("Token=" + yyToken);
450 // Report.Error (1518, lexer.Location, "Expected class, struct, interface, enum or delegate");
462 CodeGen.Assembly.AddAttributes (((Attributes)$1).Attrs);
470 global_attrs_enabled = false;
475 global_attrs_enabled = false;
484 ArrayList sect = (ArrayList) $1;
486 if (global_attrs_enabled) {
487 if (current_attr_target == "module") {
488 CodeGen.Module.AddAttributes (sect);
490 } else if (current_attr_target != null && current_attr_target.Length > 0) {
491 CodeGen.Assembly.AddAttributes (sect);
494 $$ = new Attributes (sect);
497 if (RootContext.Documentation != null) {
498 Lexer.check_incorrect_doc_comment ();
500 XmlCommentState.Allowed;
504 $$ = new Attributes (sect);
506 current_attr_target = null;
508 | attribute_sections attribute_section
510 Attributes attrs = $1 as Attributes;
511 ArrayList sect = (ArrayList) $2;
513 if (global_attrs_enabled) {
514 if (current_attr_target == "module") {
515 CodeGen.Module.AddAttributes (sect);
517 } else if (current_attr_target == "assembly") {
518 CodeGen.Assembly.AddAttributes (sect);
522 attrs = new Attributes (sect);
524 attrs.AddAttributes (sect);
528 attrs = new Attributes (sect);
530 attrs.AddAttributes (sect);
533 current_attr_target = null;
538 : OPEN_BRACKET attribute_target_specifier attribute_list opt_comma CLOSE_BRACKET
542 | OPEN_BRACKET attribute_list opt_comma CLOSE_BRACKET
548 attribute_target_specifier
549 : attribute_target COLON
551 current_attr_target = (string)$1;
559 CheckAttributeTarget ((string) $1);
562 | EVENT { $$ = "event"; }
563 | RETURN { $$ = "return"; }
569 ArrayList attrs = new ArrayList (4);
575 | attribute_list COMMA attribute
577 ArrayList attrs = (ArrayList) $1;
589 opt_attribute_arguments
591 Location loc = (Location) $2;
592 MemberName mname = (MemberName) $1;
593 MemberName left = mname.Left;
594 string identifier = mname.Name;
596 Expression left_expr = left == null ? null : left.GetTypeExpression (loc);
598 if (current_attr_target == "assembly" || current_attr_target == "module")
599 $$ = new GlobalAttribute (current_class, current_attr_target,
600 left_expr, identifier, (ArrayList) $3, loc);
602 $$ = new Attribute (current_attr_target, left_expr, identifier, (ArrayList) $3, loc);
607 : namespace_or_type_name { /* reserved attribute name or identifier: 17.4 */ }
610 opt_attribute_arguments
611 : /* empty */ { $$ = null; }
612 | OPEN_PARENS attribute_arguments CLOSE_PARENS
620 : opt_positional_argument_list
625 ArrayList args = new ArrayList (4);
631 | positional_argument_list COMMA named_argument_list
633 ArrayList args = new ArrayList (4);
639 | named_argument_list
641 ArrayList args = new ArrayList (4);
650 opt_positional_argument_list
651 : /* empty */ { $$ = null; }
652 | positional_argument_list
655 positional_argument_list
658 ArrayList args = new ArrayList (4);
659 args.Add (new Argument ((Expression) $1, Argument.AType.Expression));
663 | positional_argument_list COMMA expression
665 ArrayList args = (ArrayList) $1;
666 args.Add (new Argument ((Expression) $3, Argument.AType.Expression));
675 ArrayList args = new ArrayList (4);
680 | named_argument_list COMMA named_argument
682 ArrayList args = (ArrayList) $1;
687 | named_argument_list COMMA expression
689 Report.Error (1016, lexer.Location, "Named attribute argument expected");
695 : IDENTIFIER ASSIGN expression
697 $$ = new DictionaryEntry (
699 new Argument ((Expression) $3, Argument.AType.Expression));
705 : OPEN_BRACE opt_class_member_declarations CLOSE_BRACE
708 opt_class_member_declarations
710 | class_member_declarations
713 class_member_declarations
714 : class_member_declaration
715 | class_member_declarations
716 class_member_declaration
719 class_member_declaration
720 : constant_declaration // done
721 | field_declaration // done
722 | method_declaration // done
723 | property_declaration // done
724 | event_declaration // done
725 | indexer_declaration // done
726 | operator_declaration // done
727 | constructor_declaration // done
728 | destructor_declaration // done
738 MemberName name = MakeName ((MemberName) $5);
739 bool partial = (bool) $3;
742 ClassPart part = PartialContainer.CreatePart (
743 current_namespace, current_class, name, (int) $2,
744 (Attributes) $1, Kind.Struct, lexer.Location);
746 current_container = part.PartialContainer;
747 current_class = part;
749 current_class = new Struct (
750 current_namespace, current_class, name, (int) $2,
751 (Attributes) $1, lexer.Location);
753 current_container.AddClassOrStruct (current_class);
754 current_container = current_class;
755 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
761 current_class.Bases = (ArrayList) $7;
763 if (RootContext.Documentation != null)
764 current_class.DocComment = Lexer.consume_doc_comment ();
768 if (RootContext.Documentation != null)
769 Lexer.doc_state = XmlCommentState.Allowed;
773 $$ = pop_current_class ();
775 | opt_attributes opt_modifiers opt_partial STRUCT error {
776 CheckIdentifierToken (yyToken);
783 if (RootContext.Documentation != null)
784 Lexer.doc_state = XmlCommentState.Allowed;
786 opt_struct_member_declarations CLOSE_BRACE
789 opt_struct_member_declarations
791 | struct_member_declarations
794 struct_member_declarations
795 : struct_member_declaration
796 | struct_member_declarations struct_member_declaration
799 struct_member_declaration
800 : constant_declaration
803 | property_declaration
805 | indexer_declaration
806 | operator_declaration
807 | constructor_declaration
811 * This is only included so we can flag error 575:
812 * destructors only allowed on class types
814 | destructor_declaration
825 int modflags = (int) $2;
826 foreach (VariableDeclaration constant in (ArrayList) $5){
827 Location l = constant.Location;
828 if ((modflags & Modifiers.STATIC) != 0) {
829 Report.Error (504, l, "The constant `{0}' cannot be marked static", current_container.GetSignatureForError () + '.' + (string) constant.identifier);
833 Const c = new Const (
834 current_class, (Expression) $4, (string) constant.identifier,
835 (Expression) constant.expression_or_array_initializer, modflags,
838 if (RootContext.Documentation != null) {
839 c.DocComment = Lexer.consume_doc_comment ();
840 Lexer.doc_state = XmlCommentState.Allowed;
842 current_container.AddConstant (c);
848 : constant_declarator
850 ArrayList constants = new ArrayList (4);
855 | constant_declarators COMMA constant_declarator
858 ArrayList constants = (ArrayList) $1;
865 : IDENTIFIER ASSIGN constant_expression
867 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
871 // A const field requires a value to be provided
872 Report.Error (145, lexer.Location, "A const field requires a value to be provided");
884 Expression type = (Expression) $3;
887 foreach (VariableDeclaration var in (ArrayList) $4){
888 Field field = new Field (current_class, type, mod, var.identifier,
889 var.expression_or_array_initializer,
890 (Attributes) $1, var.Location);
892 if (RootContext.Documentation != null) {
893 field.DocComment = Lexer.consume_doc_comment ();
894 Lexer.doc_state = XmlCommentState.Allowed;
896 current_container.AddField (field);
903 fixed_variable_declarators
906 Expression type = (Expression) $4;
909 foreach (VariableDeclaration var in (ArrayList) $5) {
910 FixedField field = new FixedField (current_class, type, mod, var.identifier,
911 (Expression)var.expression_or_array_initializer, (Attributes) $1, var.Location);
913 if (RootContext.Documentation != null) {
914 field.DocComment = Lexer.consume_doc_comment ();
915 Lexer.doc_state = XmlCommentState.Allowed;
917 current_container.AddField (field);
925 Report.Error (670, lexer.Location, "Fields cannot have void type");
929 fixed_variable_declarators
930 : fixed_variable_declarator
932 ArrayList decl = new ArrayList (2);
936 | fixed_variable_declarators COMMA fixed_variable_declarator
938 ArrayList decls = (ArrayList) $1;
944 fixed_variable_declarator
945 : IDENTIFIER OPEN_BRACKET expression CLOSE_BRACKET
947 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
952 : variable_declarator
954 ArrayList decl = new ArrayList (4);
959 | variable_declarators COMMA variable_declarator
961 ArrayList decls = (ArrayList) $1;
968 : IDENTIFIER ASSIGN variable_initializer
970 $$ = new VariableDeclaration ((string) $1, $3, lexer.Location);
974 $$ = new VariableDeclaration ((string) $1, null, lexer.Location);
976 | IDENTIFIER OPEN_BRACKET opt_expression CLOSE_BRACKET
978 Report.Error (650, lexer.Location, "Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. " +
979 "To declare a fixed size buffer field, use the fixed keyword before the field type");
993 | STACKALLOC type OPEN_BRACKET expression CLOSE_BRACKET
995 $$ = new StackAlloc ((Expression) $2, (Expression) $4, lexer.Location);
999 Report.Error (1575, lexer.Location, "A stackalloc expression requires [] after type");
1006 iterator_container = (IIteratorContainer) $1;
1007 if (RootContext.Documentation != null)
1008 Lexer.doc_state = XmlCommentState.NotAllowed;
1012 Method method = (Method) $1;
1013 Block b = (Block) $3;
1014 method.Block = (ToplevelBlock) $3;
1015 current_container.AddMethod (method);
1017 current_local_parameters = null;
1018 iterator_container = null;
1020 if (RootContext.Documentation != null)
1021 Lexer.doc_state = XmlCommentState.Allowed;
1034 Report.Error (1585, lexer.Location,
1035 "Member modifier `{0}' must precede the member type and name",
1036 Modifiers.Name (i));
1047 type namespace_or_type_name
1048 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1050 MemberName name = (MemberName) $4;
1052 Method method = new Method (current_class, (Expression) $3, (int) $2,
1053 false, name, (Parameters) $6, (Attributes) $1,
1056 current_local_parameters = (Parameters) $6;
1058 if (RootContext.Documentation != null)
1059 method.DocComment = Lexer.consume_doc_comment ();
1065 VOID namespace_or_type_name
1066 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1068 MemberName name = (MemberName) $4;
1070 Method method = new Method (current_class, TypeManager.system_void_expr,
1071 (int) $2, false, name, (Parameters) $6,
1072 (Attributes) $1, lexer.Location);
1074 current_local_parameters = (Parameters) $6;
1076 if (RootContext.Documentation != null)
1077 method.DocComment = Lexer.consume_doc_comment ();
1084 modifiers namespace_or_type_name OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1086 Report.Error (1585, lexer.Location,
1087 "Member modifier `{0}' must precede the member type and name", Modifiers.Name ((int) $4));
1088 MemberName name = (MemberName) $4;
1090 Method method = new Method (current_class, TypeManager.system_void_expr,
1091 0, false, name, (Parameters) $7, (Attributes) $1,
1094 current_local_parameters = (Parameters) $7;
1096 if (RootContext.Documentation != null)
1097 method.DocComment = Lexer.consume_doc_comment ();
1105 | SEMICOLON { $$ = null; }
1108 opt_formal_parameter_list
1109 : /* empty */ { $$ = Parameters.EmptyReadOnlyParameters; }
1110 | formal_parameter_list
1113 formal_parameter_list
1116 ArrayList pars_list = (ArrayList) $1;
1118 Parameter [] pars = new Parameter [pars_list.Count];
1119 pars_list.CopyTo (pars);
1121 $$ = new Parameters (pars, null);
1123 | fixed_parameters COMMA parameter_array
1125 ArrayList pars_list = (ArrayList) $1;
1127 Parameter [] pars = new Parameter [pars_list.Count];
1128 pars_list.CopyTo (pars);
1130 $$ = new Parameters (pars, (Parameter) $3);
1132 | fixed_parameters COMMA ARGLIST
1134 ArrayList pars_list = (ArrayList) $1;
1136 Parameter [] pars = new Parameter [pars_list.Count];
1137 pars_list.CopyTo (pars);
1139 $$ = new Parameters (pars, true);
1141 | parameter_array COMMA fixed_parameters
1143 Report.Error (231, lexer.Location, "A params parameter must be the last parameter in a formal parameter list");
1146 | ARGLIST COMMA fixed_parameters
1148 Report.Error (257, lexer.Location, "An __arglist parameter must be the last parameter in a formal parameter list");
1153 $$ = new Parameters (null, (Parameter) $1);
1157 $$ = new Parameters (null, true);
1164 ArrayList pars = new ArrayList (4);
1169 | fixed_parameters COMMA fixed_parameter
1171 ArrayList pars = (ArrayList) $1;
1180 opt_parameter_modifier
1184 $$ = new Parameter ((Expression) $3, (string) $4, (Parameter.Modifier) $2, (Attributes) $1, lexer.Location);
1187 opt_parameter_modifier
1189 IDENTIFIER OPEN_BRACKET CLOSE_BRACKET
1191 Report.Error (1552, lexer.Location, "Array type specifier, [], must appear before parameter name");
1195 opt_parameter_modifier
1198 Report.Error (1001, lexer.Location, "Identifier expected");
1202 opt_parameter_modifier
1205 CheckIdentifierToken (yyToken);
1209 opt_parameter_modifier
1215 Report.Error (241, lexer.Location, "Default parameter specifiers are not permitted");
1220 opt_parameter_modifier
1221 : /* empty */ { $$ = Parameter.Modifier.NONE; }
1222 | parameter_modifier
1226 : REF { $$ = Parameter.Modifier.REF | Parameter.Modifier.ISBYREF; }
1227 | OUT { $$ = Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF; }
1231 : opt_attributes PARAMS type IDENTIFIER
1233 $$ = new Parameter ((Expression) $3, (string) $4, Parameter.Modifier.PARAMS, (Attributes) $1, lexer.Location);
1234 note ("type must be a single-dimension array type");
1236 | opt_attributes PARAMS parameter_modifier type IDENTIFIER
1238 Report.Error (1611, lexer.Location, "The params parameter cannot be declared as ref or out");
1241 | opt_attributes PARAMS type error {
1242 CheckIdentifierToken (yyToken);
1247 property_declaration
1251 namespace_or_type_name
1253 if (RootContext.Documentation != null)
1254 tmpComment = Lexer.consume_doc_comment ();
1258 implicit_value_parameter_type = (Expression) $3;
1260 lexer.PropertyParsing = true;
1262 $$ = lexer.Location;
1264 accessor_declarations
1266 lexer.PropertyParsing = false;
1267 has_get = has_set = false;
1275 Pair pair = (Pair) $8;
1276 Accessor get_block = (Accessor) pair.First;
1277 Accessor set_block = (Accessor) pair.Second;
1279 Location loc = (Location) $7;
1280 MemberName name = (MemberName) $4;
1282 prop = new Property (current_class, (Expression) $3, (int) $2, false,
1283 name, (Attributes) $1, get_block, set_block, loc);
1285 current_container.AddProperty (prop);
1286 implicit_value_parameter_type = null;
1288 if (RootContext.Documentation != null)
1289 prop.DocComment = ConsumeStoredComment ();
1294 accessor_declarations
1295 : get_accessor_declaration
1297 $$ = new Pair ($1, null);
1299 | get_accessor_declaration accessor_declarations
1301 Pair pair = (Pair) $2;
1305 | set_accessor_declaration
1307 $$ = new Pair (null, $1);
1309 | set_accessor_declaration accessor_declarations
1311 Pair pair = (Pair) $2;
1317 Report.Error (1014, lexer.Location, "A get or set accessor expected");
1322 get_accessor_declaration
1323 : opt_attributes opt_modifiers GET
1325 // If this is not the case, then current_local_parameters has already
1326 // been set in indexer_declaration
1327 if (parsing_indexer == false)
1328 current_local_parameters = null;
1330 current_local_parameters = indexer_parameters;
1331 lexer.PropertyParsing = false;
1333 iterator_container = SimpleIteratorContainer.GetSimple ();
1338 Report.Error (1007, lexer.Location, "Property accessor already defined");
1341 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1343 current_local_parameters = null;
1344 lexer.PropertyParsing = true;
1346 if (SimpleIteratorContainer.Simple.Yields)
1347 accessor.SetYields ();
1349 iterator_container = null;
1351 if (RootContext.Documentation != null)
1352 if (Lexer.doc_state == XmlCommentState.Error)
1353 Lexer.doc_state = XmlCommentState.NotAllowed;
1359 set_accessor_declaration
1360 : opt_attributes opt_modifiers SET
1363 Parameter implicit_value_parameter = new Parameter (
1364 implicit_value_parameter_type, "value",
1365 Parameter.Modifier.NONE, null, lexer.Location);
1367 if (parsing_indexer == false) {
1368 args = new Parameter [1];
1369 args [0] = implicit_value_parameter;
1370 current_local_parameters = new Parameters (args, null);
1372 Parameter [] fpars = indexer_parameters.FixedParameters;
1375 int count = fpars.Length;
1377 args = new Parameter [count + 1];
1378 fpars.CopyTo (args, 0);
1379 args [count] = implicit_value_parameter;
1382 current_local_parameters = new Parameters (
1383 args, indexer_parameters.ArrayParameter);
1386 lexer.PropertyParsing = false;
1388 iterator_container = SimpleIteratorContainer.GetSimple ();
1393 Report.Error (1007, lexer.Location, "Property accessor already defined");
1396 Accessor accessor = new Accessor ((ToplevelBlock) $5, (int) $2, (Attributes) $1, lexer.Location);
1398 current_local_parameters = null;
1399 lexer.PropertyParsing = true;
1401 if (SimpleIteratorContainer.Simple.Yields)
1402 accessor.SetYields ();
1404 iterator_container = null;
1406 if (RootContext.Documentation != null
1407 && Lexer.doc_state == XmlCommentState.Error)
1408 Lexer.doc_state = XmlCommentState.NotAllowed;
1416 | SEMICOLON { $$ = null; }
1419 interface_declaration
1423 INTERFACE member_name
1425 MemberName name = MakeName ((MemberName) $5);
1426 bool partial = (bool) $3;
1429 ClassPart part = PartialContainer.CreatePart (
1430 current_namespace, current_class, name, (int) $2,
1431 (Attributes) $1, Kind.Interface, lexer.Location);
1433 current_container = part.PartialContainer;
1434 current_class = part;
1436 current_class = new Interface (
1437 current_namespace, current_class, name, (int) $2,
1438 (Attributes) $1, lexer.Location);
1440 current_container.AddInterface (current_class);
1441 current_container = current_class;
1442 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
1447 current_class.Bases = (ArrayList) $7;
1449 if (RootContext.Documentation != null) {
1450 current_class.DocComment = Lexer.consume_doc_comment ();
1451 Lexer.doc_state = XmlCommentState.Allowed;
1456 if (RootContext.Documentation != null)
1457 Lexer.doc_state = XmlCommentState.Allowed;
1461 $$ = pop_current_class ();
1463 | opt_attributes opt_modifiers opt_partial INTERFACE error {
1464 CheckIdentifierToken (yyToken);
1470 opt_interface_member_declarations
1474 opt_interface_member_declarations
1476 | interface_member_declarations
1479 interface_member_declarations
1480 : interface_member_declaration
1481 | interface_member_declarations interface_member_declaration
1484 interface_member_declaration
1485 : interface_method_declaration
1490 Method m = (Method) $1;
1492 if (m.IsExplicitImpl)
1493 Report.Error (541, lexer.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1494 m.GetSignatureForError ());
1496 current_container.AddMethod (m);
1498 if (RootContext.Documentation != null)
1499 Lexer.doc_state = XmlCommentState.Allowed;
1501 | interface_property_declaration
1506 Property p = (Property) $1;
1508 if (p.IsExplicitImpl)
1509 Report.Error (541, lexer.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1510 p.GetSignatureForError ());
1512 current_container.AddProperty (p);
1514 if (RootContext.Documentation != null)
1515 Lexer.doc_state = XmlCommentState.Allowed;
1517 | interface_event_declaration
1520 Event e = (Event) $1;
1522 if (e.IsExplicitImpl)
1523 Report.Error (541, lexer.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1524 e.GetSignatureForError ());
1526 current_container.AddEvent (e);
1529 if (RootContext.Documentation != null)
1530 Lexer.doc_state = XmlCommentState.Allowed;
1532 | interface_indexer_declaration
1537 Indexer i = (Indexer) $1;
1539 if (i.IsExplicitImpl)
1540 Report.Error (541, lexer.Location, "`{0}': explicit interface declaration can only be declared in a class or struct",
1541 i.GetSignatureForError ());
1543 current_container.AddIndexer (i);
1545 if (RootContext.Documentation != null)
1546 Lexer.doc_state = XmlCommentState.Allowed;
1548 | delegate_declaration
1551 Report.Error (524, lexer.Location, "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1552 ((MemberCore)$1).GetSignatureForError ());
1558 Report.Error (524, lexer.Location, "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1559 ((MemberCore)$1).GetSignatureForError ());
1562 | struct_declaration
1565 Report.Error (524, lexer.Location, "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1566 ((MemberCore)$1).GetSignatureForError ());
1572 Report.Error (524, lexer.Location, "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1573 ((MemberCore)$1).GetSignatureForError ());
1576 | interface_declaration
1579 Report.Error (524, lexer.Location, "`{0}': Interfaces cannot declare classes, structs, interfaces, delegates, enumerations or constants",
1580 ((MemberCore)$1).GetSignatureForError ());
1583 | constant_declaration
1585 Report.Error (525, lexer.Location, "Interfaces cannot contain fields or constants");
1593 val = Modifiers.Check (Modifiers.NEW | Modifiers.UNSAFE, val, 0, lexer.Location);
1598 interface_method_declaration
1599 : opt_attributes opt_new type namespace_or_type_name
1600 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1603 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1604 (MemberName) $4, (Parameters) $6, (Attributes) $1, lexer.Location);
1605 if (RootContext.Documentation != null)
1606 ((Method) $$).DocComment = Lexer.consume_doc_comment ();
1608 | opt_attributes opt_new type namespace_or_type_name
1609 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
1610 OPEN_BRACE opt_statement_list CLOSE_BRACE
1612 $$ = new Method (current_class, (Expression) $3, (int) $2, true,
1613 (MemberName) $4, (Parameters) $6, (Attributes) $1, lexer.Location);
1615 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition",
1616 ((Method)$$).GetSignatureForError ());
1620 interface_property_declaration
1625 { lexer.PropertyParsing = true; }
1627 { lexer.PropertyParsing = false; }
1630 if ($3 == TypeManager.system_void_expr) {
1631 Report.Error (547, lexer.Location, "`{0}': property or indexer cannot have void type", $4);
1636 Property p = new Property (current_class, (Expression) $3, (int) $2, true,
1637 new MemberName ((string) $4), (Attributes) $1,
1638 null, null, lexer.Location);
1640 Report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", p.GetSignatureForError ());
1644 InterfaceAccessorInfo pinfo = (InterfaceAccessorInfo) $7;
1646 $$ = new Property (current_class, (Expression) $3, (int) $2, true,
1647 new MemberName ((string) $4), (Attributes) $1,
1648 pinfo.Get, pinfo.Set, lexer.Location);
1649 if (RootContext.Documentation != null)
1650 ((Property) $$).DocComment = Lexer.consume_doc_comment ();
1655 CheckIdentifierToken (yyToken);
1661 : opt_attributes opt_modifiers GET SEMICOLON
1662 { $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location); }
1663 | opt_attributes opt_modifiers GET OPEN_BRACE
1665 Report.Error (531, lexer.Location, "`{0}': interface members cannot have a definition", ".get");
1666 $$ = new InterfaceAccessorInfo (true, false, (Attributes) $1, null, (int) $2, 0, lexer.Location, lexer.Location);
1668 | opt_attributes opt_modifiers SET SEMICOLON
1669 { $$ = new InterfaceAccessorInfo (false, true, null, (Attributes) $1, 0, (int) $2, lexer.Location, lexer.Location); }
1670 | opt_attributes opt_modifiers GET SEMICOLON opt_attributes opt_modifiers SET SEMICOLON
1671 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $1, (Attributes) $5, (int) $2, (int) $6, lexer.Location, lexer.Location); }
1672 | opt_attributes opt_modifiers SET SEMICOLON opt_attributes opt_modifiers GET SEMICOLON
1673 { $$ = new InterfaceAccessorInfo (true, true, (Attributes) $5, (Attributes) $1, (int) $6, (int) $2, lexer.Location, lexer.Location); }
1680 interface_event_declaration
1681 : opt_attributes opt_new EVENT type IDENTIFIER SEMICOLON
1683 $$ = new EventField (current_class, (Expression) $4, (int) $2, true,
1684 new MemberName ((string) $5), null,
1685 (Attributes) $1, lexer.Location);
1686 if (RootContext.Documentation != null)
1687 ((EventField) $$).DocComment = Lexer.consume_doc_comment ();
1689 | opt_attributes opt_new EVENT type error {
1690 CheckIdentifierToken (yyToken);
1693 | opt_attributes opt_new EVENT type IDENTIFIER ASSIGN {
1694 Report.Error (68, lexer.Location, "`{0}.{1}': event in interface cannot have initializer", current_container.Name, $5);
1697 | opt_attributes opt_new EVENT type IDENTIFIER OPEN_BRACE
1699 lexer.EventParsing = true;
1701 event_accessor_declarations
1703 lexer.EventParsing = false;
1706 Report.Error (69, lexer.Location, "Event in interface cannot have add or remove accessors");
1711 interface_indexer_declaration
1712 : opt_attributes opt_new type THIS
1713 OPEN_BRACKET formal_parameter_list CLOSE_BRACKET
1715 { lexer.PropertyParsing = true; }
1717 { lexer.PropertyParsing = false; }
1721 Indexer i = new Indexer (current_class, (Expression) $3,
1722 new MemberName (TypeContainer.DefaultIndexerName),
1723 (int) $2, true, (Parameters) $6, (Attributes) $1,
1724 null, null, lexer.Location);
1726 Report.Error (548, lexer.Location, "`{0}': property or indexer must have at least one accessor", i.GetSignatureForError ());
1730 InterfaceAccessorInfo info = (InterfaceAccessorInfo) $10;
1732 $$ = new Indexer (current_class, (Expression) $3,
1733 new MemberName (TypeContainer.DefaultIndexerName),
1734 (int) $2, true, (Parameters) $6, (Attributes) $1,
1735 info.Get, info.Set, lexer.Location);
1736 if (RootContext.Documentation != null)
1737 ((Indexer) $$).DocComment = ConsumeStoredComment ();
1741 operator_declaration
1742 : opt_attributes opt_modifiers operator_declarator
1744 iterator_container = SimpleIteratorContainer.GetSimple ();
1751 OperatorDeclaration decl = (OperatorDeclaration) $3;
1753 Parameter [] param_list = new Parameter [decl.arg2type != null ? 2 : 1];
1755 param_list[0] = new Parameter (decl.arg1type, decl.arg1name, Parameter.Modifier.NONE, null, decl.location);
1756 if (decl.arg2type != null)
1757 param_list[1] = new Parameter (decl.arg2type, decl.arg2name, Parameter.Modifier.NONE, null, decl.location);
1759 Operator op = new Operator (
1760 current_class, decl.optype, decl.ret_type, (int) $2,
1761 new Parameters (param_list, null),
1762 (ToplevelBlock) $5, (Attributes) $1, decl.location);
1764 if (RootContext.Documentation != null) {
1765 op.DocComment = tmpComment;
1766 Lexer.doc_state = XmlCommentState.Allowed;
1769 if (SimpleIteratorContainer.Simple.Yields)
1772 // Note again, checking is done in semantic analysis
1773 current_container.AddOperator (op);
1775 current_local_parameters = null;
1776 iterator_container = null;
1782 | SEMICOLON { $$ = null; }
1785 : type OPERATOR overloadable_operator
1786 OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1788 Operator.OpType op = (Operator.OpType) $3;
1789 CheckUnaryOperator (op);
1791 if (op == Operator.OpType.Addition)
1792 op = Operator.OpType.UnaryPlus;
1794 if (op == Operator.OpType.Subtraction)
1795 op = Operator.OpType.UnaryNegation;
1797 Parameter [] pars = new Parameter [1];
1798 Expression type = (Expression) $5;
1800 pars [0] = new Parameter (type, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1802 current_local_parameters = new Parameters (pars, null);
1804 if (RootContext.Documentation != null) {
1805 tmpComment = Lexer.consume_doc_comment ();
1806 Lexer.doc_state = XmlCommentState.NotAllowed;
1809 $$ = new OperatorDeclaration (op, (Expression) $1, type, (string) $6,
1810 null, null, lexer.Location);
1812 | type OPERATOR overloadable_operator
1814 type IDENTIFIER COMMA
1818 CheckBinaryOperator ((Operator.OpType) $3);
1820 Parameter [] pars = new Parameter [2];
1822 Expression typeL = (Expression) $5;
1823 Expression typeR = (Expression) $8;
1825 pars [0] = new Parameter (typeL, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1826 pars [1] = new Parameter (typeR, (string) $9, Parameter.Modifier.NONE, null, lexer.Location);
1828 current_local_parameters = new Parameters (pars, null);
1830 if (RootContext.Documentation != null) {
1831 tmpComment = Lexer.consume_doc_comment ();
1832 Lexer.doc_state = XmlCommentState.NotAllowed;
1835 $$ = new OperatorDeclaration ((Operator.OpType) $3, (Expression) $1,
1837 typeR, (string) $9, lexer.Location);
1839 | conversion_operator_declarator
1840 | type OPERATOR overloadable_operator
1842 type IDENTIFIER COMMA
1843 type IDENTIFIER COMMA
1847 Report.Error (1534, lexer.Location, "Overloaded binary operator `{0}' takes two parameters",
1848 Operator.GetName ((Operator.OpType) $3));
1851 | type OPERATOR overloadable_operator
1852 OPEN_PARENS CLOSE_PARENS
1854 Report.Error (1535, lexer.Location, "Overloaded unary operator `{0}' takes one parameter",
1855 Operator.GetName ((Operator.OpType) $3));
1860 overloadable_operator
1862 : BANG { $$ = Operator.OpType.LogicalNot; }
1863 | TILDE { $$ = Operator.OpType.OnesComplement; }
1864 | OP_INC { $$ = Operator.OpType.Increment; }
1865 | OP_DEC { $$ = Operator.OpType.Decrement; }
1866 | TRUE { $$ = Operator.OpType.True; }
1867 | FALSE { $$ = Operator.OpType.False; }
1868 // Unary and binary:
1869 | PLUS { $$ = Operator.OpType.Addition; }
1870 | MINUS { $$ = Operator.OpType.Subtraction; }
1872 | STAR { $$ = Operator.OpType.Multiply; }
1873 | DIV { $$ = Operator.OpType.Division; }
1874 | PERCENT { $$ = Operator.OpType.Modulus; }
1875 | BITWISE_AND { $$ = Operator.OpType.BitwiseAnd; }
1876 | BITWISE_OR { $$ = Operator.OpType.BitwiseOr; }
1877 | CARRET { $$ = Operator.OpType.ExclusiveOr; }
1878 | OP_SHIFT_LEFT { $$ = Operator.OpType.LeftShift; }
1879 | OP_SHIFT_RIGHT { $$ = Operator.OpType.RightShift; }
1880 | OP_EQ { $$ = Operator.OpType.Equality; }
1881 | OP_NE { $$ = Operator.OpType.Inequality; }
1882 | OP_GT { $$ = Operator.OpType.GreaterThan; }
1883 | OP_LT { $$ = Operator.OpType.LessThan; }
1884 | OP_GE { $$ = Operator.OpType.GreaterThanOrEqual; }
1885 | OP_LE { $$ = Operator.OpType.LessThanOrEqual; }
1888 conversion_operator_declarator
1889 : IMPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1891 Parameter [] pars = new Parameter [1];
1893 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1895 current_local_parameters = new Parameters (pars, null);
1897 if (RootContext.Documentation != null) {
1898 tmpComment = Lexer.consume_doc_comment ();
1899 Lexer.doc_state = XmlCommentState.NotAllowed;
1902 $$ = new OperatorDeclaration (Operator.OpType.Implicit, (Expression) $3, (Expression) $5, (string) $6,
1903 null, null, lexer.Location);
1905 | EXPLICIT OPERATOR type OPEN_PARENS type IDENTIFIER CLOSE_PARENS
1907 Parameter [] pars = new Parameter [1];
1909 pars [0] = new Parameter ((Expression) $5, (string) $6, Parameter.Modifier.NONE, null, lexer.Location);
1911 current_local_parameters = new Parameters (pars, null);
1913 if (RootContext.Documentation != null) {
1914 tmpComment = Lexer.consume_doc_comment ();
1915 Lexer.doc_state = XmlCommentState.NotAllowed;
1918 $$ = new OperatorDeclaration (Operator.OpType.Explicit, (Expression) $3, (Expression) $5, (string) $6,
1919 null, null, lexer.Location);
1923 syntax_error (lexer.Location, "'operator' expected");
1927 syntax_error (lexer.Location, "'operator' expected");
1931 constructor_declaration
1934 constructor_declarator
1937 Constructor c = (Constructor) $3;
1938 c.Block = (ToplevelBlock) $4;
1939 c.OptAttributes = (Attributes) $1;
1940 c.ModFlags = (int) $2;
1942 if (RootContext.Documentation != null)
1943 c.DocComment = ConsumeStoredComment ();
1945 if (c.Name == current_container.Basename){
1946 if ((c.ModFlags & Modifiers.STATIC) != 0){
1947 if ((c.ModFlags & Modifiers.Accessibility) != 0){
1948 Report.Error (515, c.Location,
1949 "`{0}': access modifiers are not allowed on static constructors",
1950 c.GetSignatureForError ());
1953 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1955 if (c.Initializer != null){
1956 Report.Error (514, c.Location,
1957 "`{0}': static constructor cannot have an explicit `this' or `base' constructor call",
1958 c.GetSignatureForError ());
1961 if (!c.Parameters.Empty){
1962 Report.Error (132, c.Location,
1963 "`{0}': The static constructor must be parameterless", c.GetSignatureForError ());
1966 c.ModFlags = Modifiers.Check (Constructor.AllowedModifiers, (int) $2, Modifiers.PRIVATE, c.Location);
1969 // We let another layer check the validity of the constructor.
1970 //Console.WriteLine ("{0} and {1}", c.Name, current_container.Basename);
1973 current_container.AddConstructor (c);
1975 current_local_parameters = null;
1976 if (RootContext.Documentation != null)
1977 Lexer.doc_state = XmlCommentState.Allowed;
1981 constructor_declarator
1984 if (RootContext.Documentation != null) {
1985 tmpComment = Lexer.consume_doc_comment ();
1986 Lexer.doc_state = XmlCommentState.Allowed;
1989 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS _mark_
1991 current_local_parameters = (Parameters) $4;
1993 opt_constructor_initializer
1995 $$ = new Constructor (current_class, (string) $1, 0, (Parameters) $4,
1996 (ConstructorInitializer) $8, (Location) $6);
2002 | SEMICOLON { $$ = null; }
2005 opt_constructor_initializer
2006 : /* empty */ { $$ = null; }
2007 | constructor_initializer
2010 constructor_initializer
2011 : COLON BASE OPEN_PARENS opt_argument_list CLOSE_PARENS
2013 $$ = new ConstructorBaseInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2015 | COLON THIS OPEN_PARENS opt_argument_list CLOSE_PARENS
2017 $$ = new ConstructorThisInitializer ((ArrayList) $4, current_local_parameters, lexer.Location);
2020 Report.Error (1018, lexer.Location, "Keyword this or base expected");
2026 : /* EMPTY */ { $$ = 0; }
2027 | UNSAFE { $$ = Modifiers.UNSAFE; }
2028 | EXTERN { $$ = Modifiers.EXTERN; }
2031 destructor_declaration
2032 : opt_attributes opt_finalizer TILDE
2034 if (RootContext.Documentation != null) {
2035 tmpComment = Lexer.consume_doc_comment ();
2036 Lexer.doc_state = XmlCommentState.NotAllowed;
2039 IDENTIFIER OPEN_PARENS CLOSE_PARENS block
2041 if ((string) $5 != current_container.Basename){
2042 Report.Error (574, lexer.Location, "Name of destructor must match name of class");
2043 } else if (current_container.Kind != Kind.Class){
2044 Report.Error (575, lexer.Location, "Only class types can contain destructor");
2046 Location l = lexer.Location;
2049 if (!RootContext.StdLib && current_container.Name == "System.Object")
2050 m |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
2052 m |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2054 Method d = new Destructor (
2055 current_class, TypeManager.system_void_expr, m, "Finalize",
2056 new Parameters (null, null), (Attributes) $1, l);
2057 if (RootContext.Documentation != null)
2058 d.DocComment = ConsumeStoredComment ();
2060 d.Block = (ToplevelBlock) $8;
2061 current_container.AddMethod (d);
2069 EVENT type variable_declarators SEMICOLON
2071 foreach (VariableDeclaration var in (ArrayList) $5) {
2073 MemberName name = new MemberName (var.identifier);
2075 Event e = new EventField (
2076 current_class, (Expression) $4, (int) $2, false, name,
2077 var.expression_or_array_initializer, (Attributes) $1,
2080 current_container.AddEvent (e);
2082 if (RootContext.Documentation != null) {
2083 e.DocComment = Lexer.consume_doc_comment ();
2084 Lexer.doc_state = XmlCommentState.Allowed;
2090 EVENT type namespace_or_type_name
2093 implicit_value_parameter_type = (Expression) $4;
2094 lexer.EventParsing = true;
2096 event_accessor_declarations
2098 lexer.EventParsing = false;
2102 Location loc = (Location) $7;
2103 MemberName name = (MemberName) $5;
2106 Report.Error (65, lexer.Location, "`{0}.{1}': event property must have both add and remove accessors",
2107 current_container.Name, name);
2110 Pair pair = (Pair) $9;
2112 Event e = new EventProperty (
2113 current_class, (Expression) $4, (int) $2, false, name, null,
2114 (Attributes) $1, (Accessor) pair.First, (Accessor) pair.Second,
2116 if (RootContext.Documentation != null) {
2117 e.DocComment = Lexer.consume_doc_comment ();
2118 Lexer.doc_state = XmlCommentState.Allowed;
2121 current_container.AddEvent (e);
2122 implicit_value_parameter_type = null;
2125 | opt_attributes opt_modifiers EVENT type namespace_or_type_name error {
2126 MemberName mn = (MemberName) $5;
2128 if (mn.Left != null)
2129 Report.Error (71, lexer.Location, "An explicit interface implementation of an event must use property syntax");
2131 Report.Error (71, lexer.Location, "Event declaration should use property syntax");
2133 if (RootContext.Documentation != null)
2134 Lexer.doc_state = XmlCommentState.Allowed;
2138 event_accessor_declarations
2139 : add_accessor_declaration remove_accessor_declaration
2141 $$ = new Pair ($1, $2);
2143 | remove_accessor_declaration add_accessor_declaration
2145 $$ = new Pair ($2, $1);
2147 | add_accessor_declaration { $$ = null; }
2148 | remove_accessor_declaration { $$ = null; }
2151 Report.Error (1055, lexer.Location, "An add or remove accessor expected");
2157 add_accessor_declaration
2158 : opt_attributes ADD
2160 Parameter [] args = new Parameter [1];
2161 Parameter implicit_value_parameter = new Parameter (
2162 implicit_value_parameter_type, "value",
2163 Parameter.Modifier.NONE, null, lexer.Location);
2165 args [0] = implicit_value_parameter;
2167 current_local_parameters = new Parameters (args, null);
2168 lexer.EventParsing = false;
2172 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2173 lexer.EventParsing = true;
2175 | opt_attributes ADD error {
2176 Report.Error (73, lexer.Location, "An add or remove accessor must have a body");
2179 | opt_attributes modifiers ADD {
2180 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2185 remove_accessor_declaration
2186 : opt_attributes REMOVE
2188 Parameter [] args = new Parameter [1];
2189 Parameter implicit_value_parameter = new Parameter (
2190 implicit_value_parameter_type, "value",
2191 Parameter.Modifier.NONE, null, lexer.Location);
2193 args [0] = implicit_value_parameter;
2195 current_local_parameters = new Parameters (args, null);
2196 lexer.EventParsing = false;
2200 $$ = new Accessor ((ToplevelBlock) $4, 0, (Attributes) $1, lexer.Location);
2201 lexer.EventParsing = true;
2203 | opt_attributes REMOVE error {
2204 Report.Error (73, lexer.Location, "An add or remove accessor must have a body");
2207 | opt_attributes modifiers REMOVE {
2208 Report.Error (1609, lexer.Location, "Modifiers cannot be placed on event accessor declarations");
2214 : opt_attributes opt_modifiers indexer_declarator
2217 IndexerDeclaration decl = (IndexerDeclaration) $3;
2219 implicit_value_parameter_type = decl.type;
2221 lexer.PropertyParsing = true;
2222 parsing_indexer = true;
2224 indexer_parameters = decl.param_list;
2226 accessor_declarations
2228 lexer.PropertyParsing = false;
2229 has_get = has_set = false;
2230 parsing_indexer = false;
2237 // The signature is computed from the signature of the indexer. Look
2238 // at section 3.6 on the spec
2239 Location loc = (Location) $5;
2241 IndexerDeclaration decl = (IndexerDeclaration) $3;
2242 Pair pair = (Pair) $7;
2243 Accessor get_block = (Accessor) pair.First;
2244 Accessor set_block = (Accessor) pair.Second;
2247 if (decl.interface_type != null)
2248 name = new MemberName (decl.interface_type,
2249 TypeContainer.DefaultIndexerName);
2251 name = new MemberName (TypeContainer.DefaultIndexerName);
2253 indexer = new Indexer (current_class, decl.type, name,
2254 (int) $2, false, decl.param_list, (Attributes) $1,
2255 get_block, set_block, loc);
2257 if (RootContext.Documentation != null)
2258 indexer.DocComment = ConsumeStoredComment ();
2260 current_container.AddIndexer (indexer);
2262 current_local_parameters = null;
2263 implicit_value_parameter_type = null;
2264 indexer_parameters = null;
2269 : type THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2271 Parameters pars = (Parameters) $4;
2272 if (pars.HasArglist) {
2273 // "__arglist is not valid in this context"
2274 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2275 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2276 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2278 if (RootContext.Documentation != null) {
2279 tmpComment = Lexer.consume_doc_comment ();
2280 Lexer.doc_state = XmlCommentState.Allowed;
2283 $$ = new IndexerDeclaration ((Expression) $1, null, pars);
2285 | type namespace_or_type_name DOT THIS OPEN_BRACKET opt_formal_parameter_list CLOSE_BRACKET
2287 Parameters pars = (Parameters) $6;
2289 if (pars.HasArglist) {
2290 // "__arglist is not valid in this context"
2291 Report.Error (1669, lexer.Location, "__arglist is not valid in this context");
2292 } else if (pars.FixedParameters == null && pars.ArrayParameter == null){
2293 Report.Error (1551, lexer.Location, "Indexers must have at least one parameter");
2296 MemberName name = (MemberName) $2;
2297 $$ = new IndexerDeclaration ((Expression) $1, name, pars);
2299 if (RootContext.Documentation != null) {
2300 tmpComment = Lexer.consume_doc_comment ();
2301 Lexer.doc_state = XmlCommentState.Allowed;
2312 if (RootContext.Documentation != null)
2313 enumTypeComment = Lexer.consume_doc_comment ();
2318 bool partial = (bool) $3;
2321 Report.Error (267, lexer.Location, "The partial modifier can only appear immediately before `class', `struct' or `interface'");
2322 break; // assumes that the parser put us in a switch
2325 Location enum_location = lexer.Location;
2327 MemberName name = MakeName (new MemberName ((string) $5));
2328 Enum e = new Enum (current_namespace, current_class, (Expression) $6, (int) $2,
2329 name, (Attributes) $1, enum_location);
2331 if (RootContext.Documentation != null)
2332 e.DocComment = enumTypeComment;
2334 foreach (VariableDeclaration ev in (ArrayList) $8) {
2335 e.AddEnumMember (ev.identifier,
2336 (Expression) ev.expression_or_array_initializer,
2337 ev.Location, ev.OptAttributes,
2341 current_container.AddEnum (e);
2342 RootContext.Tree.RecordDecl (current_namespace.NS, name, e);
2349 : /* empty */ { $$ = TypeManager.system_int32_expr; }
2350 | COLON type { $$ = $2; }
2356 if (RootContext.Documentation != null)
2357 Lexer.doc_state = XmlCommentState.Allowed;
2359 opt_enum_member_declarations
2361 // here will be evaluated after CLOSE_BLACE is consumed.
2362 if (RootContext.Documentation != null)
2363 Lexer.doc_state = XmlCommentState.Allowed;
2371 opt_enum_member_declarations
2372 : /* empty */ { $$ = new ArrayList (4); }
2373 | enum_member_declarations opt_comma { $$ = $1; }
2376 enum_member_declarations
2377 : enum_member_declaration
2379 ArrayList l = new ArrayList (4);
2384 | enum_member_declarations COMMA enum_member_declaration
2386 ArrayList l = (ArrayList) $1;
2394 enum_member_declaration
2395 : opt_attributes IDENTIFIER
2397 VariableDeclaration vd = new VariableDeclaration ((string) $2, null, lexer.Location, (Attributes) $1);
2399 if (RootContext.Documentation != null) {
2400 vd.DocComment = Lexer.consume_doc_comment ();
2401 Lexer.doc_state = XmlCommentState.Allowed;
2406 | opt_attributes IDENTIFIER
2408 $$ = lexer.Location;
2409 if (RootContext.Documentation != null) {
2410 tmpComment = Lexer.consume_doc_comment ();
2411 Lexer.doc_state = XmlCommentState.NotAllowed;
2416 VariableDeclaration vd = new VariableDeclaration ((string) $2, $5, lexer.Location, (Attributes) $1);
2418 if (RootContext.Documentation != null)
2419 vd.DocComment = ConsumeStoredComment ();
2425 delegate_declaration
2428 DELEGATE type member_name
2429 OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
2432 Location l = lexer.Location;
2433 MemberName name = MakeName ((MemberName) $5);
2434 Delegate del = new Delegate (current_namespace, current_class, (Expression) $4,
2435 (int) $2, name, (Parameters) $7, (Attributes) $1, l);
2437 if (RootContext.Documentation != null) {
2438 del.DocComment = Lexer.consume_doc_comment ();
2439 Lexer.doc_state = XmlCommentState.Allowed;
2442 current_container.AddDelegate (del);
2443 RootContext.Tree.RecordDecl (current_namespace.NS, name, del);
2448 namespace_or_type_name
2450 | namespace_or_type_name DOT IDENTIFIER {
2451 $$ = new MemberName ((MemberName) $1, (string) $3);
2457 $$ = new MemberName ((string) $1);
2462 * Before you think of adding a return_type, notice that we have been
2463 * using two rules in the places where it matters (one rule using type
2464 * and another identical one that uses VOID as the return type). This
2465 * gets rid of a shift/reduce couple
2468 : namespace_or_type_name
2470 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2482 // Note that here only unmanaged types are allowed but we
2483 // can't perform checks during this phase - we do it during
2484 // semantic analysis.
2486 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2490 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);
2496 | non_expression_type rank_specifier
2498 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2500 | non_expression_type STAR
2502 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2504 | expression rank_specifiers
2506 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2510 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2514 // We need this because the parser will happily go and reduce IDENTIFIER STAR
2515 // through this different path
2517 | multiplicative_expression STAR
2519 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
2526 ArrayList types = new ArrayList (4);
2531 | type_list COMMA type
2533 ArrayList types = (ArrayList) $1;
2541 * replaces all the productions for isolating the various
2542 * simple types, but we need this to reuse it easily in local_variable_type
2545 : OBJECT { $$ = TypeManager.system_object_expr; }
2546 | STRING { $$ = TypeManager.system_string_expr; }
2547 | BOOL { $$ = TypeManager.system_boolean_expr; }
2548 | DECIMAL { $$ = TypeManager.system_decimal_expr; }
2549 | FLOAT { $$ = TypeManager.system_single_expr; }
2550 | DOUBLE { $$ = TypeManager.system_double_expr; }
2555 : SBYTE { $$ = TypeManager.system_sbyte_expr; }
2556 | BYTE { $$ = TypeManager.system_byte_expr; }
2557 | SHORT { $$ = TypeManager.system_int16_expr; }
2558 | USHORT { $$ = TypeManager.system_uint16_expr; }
2559 | INT { $$ = TypeManager.system_int32_expr; }
2560 | UINT { $$ = TypeManager.system_uint32_expr; }
2561 | LONG { $$ = TypeManager.system_int64_expr; }
2562 | ULONG { $$ = TypeManager.system_uint64_expr; }
2563 | CHAR { $$ = TypeManager.system_char_expr; }
2564 | VOID { $$ = TypeManager.system_void_expr; }
2568 : type rank_specifiers
2570 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
2575 // Expressions, section 7.5
2585 $$ = ((MemberName) $1).GetTypeExpression (lexer.Location);
2587 | parenthesized_expression
2589 | invocation_expression
2593 | post_increment_expression
2594 | post_decrement_expression
2598 | checked_expression
2599 | unchecked_expression
2600 | pointer_member_access
2601 | anonymous_method_expression
2608 | LITERAL_CHARACTER { $$ = new CharLiteral ((char) lexer.Value); }
2609 | LITERAL_STRING { $$ = new StringLiteral ((string) lexer.Value); }
2610 | NULL { $$ = NullLiteral.Null; }
2614 : LITERAL_FLOAT { $$ = new FloatLiteral ((float) lexer.Value); }
2615 | LITERAL_DOUBLE { $$ = new DoubleLiteral ((double) lexer.Value); }
2616 | LITERAL_DECIMAL { $$ = new DecimalLiteral ((decimal) lexer.Value); }
2621 object v = lexer.Value;
2627 $$ = IntLiteral.Zero;
2629 $$ = IntLiteral.One;
2631 $$ = new IntLiteral (i);
2632 } else if (v is uint)
2633 $$ = new UIntLiteral ((UInt32) v);
2635 $$ = new LongLiteral ((Int64) v);
2636 else if (v is ulong)
2637 $$ = new ULongLiteral ((UInt64) v);
2639 Console.WriteLine ("OOPS. Unexpected result from scanner");
2644 : TRUE { $$ = new BoolLiteral (true); }
2645 | FALSE { $$ = new BoolLiteral (false); }
2648 parenthesized_expression_0
2649 : OPEN_PARENS expression CLOSE_PARENS
2652 lexer.Deambiguate_CloseParens ();
2653 // After this, the next token returned is one of
2654 // CLOSE_PARENS_CAST, CLOSE_PARENS_NO_CAST, CLOSE_PARENS_OPEN_PARENS
2655 // or CLOSE_PARENS_MINUS.
2659 parenthesized_expression
2660 : parenthesized_expression_0 CLOSE_PARENS_NO_CAST
2664 | parenthesized_expression_0 CLOSE_PARENS_MINUS
2666 // If a parenthesized expression is followed by a minus, we need to wrap
2667 // the expression inside a ParenthesizedExpression for the CS0075 check
2668 // in Binary.DoResolve().
2669 $$ = new ParenthesizedExpression ((Expression) $1, lexer.Location);
2674 : primary_expression DOT IDENTIFIER
2676 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2678 | predefined_type DOT IDENTIFIER
2680 $$ = new MemberAccess ((Expression) $1, (string) $3, lexer.Location);
2688 invocation_expression
2689 : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
2692 Location l = lexer.Location;
2693 Report.Error (1, l, "Parse error");
2695 $$ = new Invocation ((Expression) $1, (ArrayList) $3, lexer.Location);
2697 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS CLOSE_PARENS
2699 $$ = new Invocation ((Expression) $1, new ArrayList (), lexer.Location);
2701 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS primary_expression
2703 $$ = new InvocationOrCast ((Expression) $1, (Expression) $3, lexer.Location);
2705 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS non_simple_argument CLOSE_PARENS
2707 ArrayList args = new ArrayList (1);
2709 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2711 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS OPEN_PARENS argument_list COMMA argument CLOSE_PARENS
2713 ArrayList args = ((ArrayList) $4);
2715 $$ = new Invocation ((Expression) $1, args, lexer.Location);
2720 : /* empty */ { $$ = null; }
2727 ArrayList list = new ArrayList (4);
2731 | argument_list COMMA argument
2733 ArrayList list = (ArrayList) $1;
2737 | argument_list error {
2738 CheckToken (1026, yyToken, "Expected `,' or `)'");
2745 $$ = new Argument ((Expression) $1, Argument.AType.Expression);
2747 | non_simple_argument
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)){
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, "A 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;
2984 : TYPEOF OPEN_PARENS VOID CLOSE_PARENS
2986 $$ = new TypeOfVoid (lexer.Location);
2988 | TYPEOF OPEN_PARENS type CLOSE_PARENS
2990 $$ = new TypeOf ((Expression) $3, lexer.Location);
2995 : SIZEOF OPEN_PARENS type CLOSE_PARENS {
2996 $$ = new SizeOf ((Expression) $3, lexer.Location);
3001 : CHECKED OPEN_PARENS expression CLOSE_PARENS
3003 $$ = new CheckedExpr ((Expression) $3, lexer.Location);
3007 unchecked_expression
3008 : UNCHECKED OPEN_PARENS expression CLOSE_PARENS
3010 $$ = new UnCheckedExpr ((Expression) $3, lexer.Location);
3014 pointer_member_access
3015 : primary_expression OP_PTR IDENTIFIER
3019 deref = new Unary (Unary.Operator.Indirection, (Expression) $1, lexer.Location);
3020 $$ = new MemberAccess (deref, (string) $3, lexer.Location);
3024 anonymous_method_expression
3025 : DELEGATE opt_anonymous_method_signature _mark_
3027 oob_stack.Push (current_local_parameters);
3028 current_local_parameters = (Parameters)$2;
3030 // Force the next block to be created as a ToplevelBlock
3031 oob_stack.Push (current_block);
3032 oob_stack.Push (top_current_block);
3033 current_block = null;
3037 Location loc = (Location) $3;
3038 top_current_block = (Block) oob_stack.Pop ();
3039 current_block = (Block) oob_stack.Pop ();
3040 if (RootContext.Version == LanguageVersion.ISO_1){
3041 Report.FeatureIsNotStandardized (lexer.Location, "anonymous methods");
3044 ToplevelBlock anon_block = (ToplevelBlock) $5;
3046 anon_block.Parent = current_block;
3047 $$ = new AnonymousMethod ((Parameters) $2, (ToplevelBlock) top_current_block,
3050 current_local_parameters = (Parameters) oob_stack.Pop ();
3054 opt_anonymous_method_signature
3055 : /* empty */ { $$ = null; }
3056 | anonymous_method_signature
3059 anonymous_method_signature
3060 : OPEN_PARENS opt_anonymous_method_parameter_list CLOSE_PARENS
3063 $$ = Parameters.EmptyReadOnlyParameters;
3065 ArrayList par_list = (ArrayList) $2;
3066 Parameter [] pars = new Parameter [par_list.Count];
3067 par_list.CopyTo (pars);
3068 $$ = new Parameters (pars, null);
3073 opt_anonymous_method_parameter_list
3074 : /* empty */ { $$ = null; }
3075 | anonymous_method_parameter_list { $$ = $1; }
3078 anonymous_method_parameter_list
3079 : anonymous_method_parameter
3081 ArrayList a = new ArrayList (4);
3085 | anonymous_method_parameter_list COMMA anonymous_method_parameter
3087 ArrayList a = (ArrayList) $1;
3093 anonymous_method_parameter
3094 : opt_parameter_modifier type IDENTIFIER {
3095 $$ = new Parameter ((Expression) $2, (string) $3, (Parameter.Modifier) $1, null, lexer.Location);
3097 | PARAMS type IDENTIFIER {
3098 Report.Error (1670, lexer.Location, "The `params' modifier is not allowed in anonymous method declaration");
3104 : primary_expression
3105 | BANG prefixed_unary_expression
3107 $$ = new Unary (Unary.Operator.LogicalNot, (Expression) $2, lexer.Location);
3109 | TILDE prefixed_unary_expression
3111 $$ = new Unary (Unary.Operator.OnesComplement, (Expression) $2, lexer.Location);
3117 : parenthesized_expression_0 CLOSE_PARENS_CAST unary_expression
3119 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3121 | parenthesized_expression_0 CLOSE_PARENS_OPEN_PARENS cast_expression
3123 $$ = new Cast ((Expression) $1, (Expression) $3, lexer.Location);
3129 | OPEN_PARENS non_expression_type CLOSE_PARENS prefixed_unary_expression
3131 $$ = new Cast ((Expression) $2, (Expression) $4, lexer.Location);
3136 // The idea to split this out is from Rhys' grammar
3137 // to solve the problem with casts.
3139 prefixed_unary_expression
3141 | PLUS prefixed_unary_expression
3143 $$ = new Unary (Unary.Operator.UnaryPlus, (Expression) $2, lexer.Location);
3145 | MINUS prefixed_unary_expression
3147 $$ = new Unary (Unary.Operator.UnaryNegation, (Expression) $2, lexer.Location);
3149 | OP_INC prefixed_unary_expression
3151 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3152 (Expression) $2, lexer.Location);
3154 | OP_DEC prefixed_unary_expression
3156 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3157 (Expression) $2, lexer.Location);
3159 | STAR prefixed_unary_expression
3161 $$ = new Unary (Unary.Operator.Indirection, (Expression) $2, lexer.Location);
3163 | BITWISE_AND prefixed_unary_expression
3165 $$ = new Unary (Unary.Operator.AddressOf, (Expression) $2, lexer.Location);
3169 pre_increment_expression
3170 : OP_INC prefixed_unary_expression
3172 $$ = new UnaryMutator (UnaryMutator.Mode.PreIncrement,
3173 (Expression) $2, lexer.Location);
3177 pre_decrement_expression
3178 : OP_DEC prefixed_unary_expression
3180 $$ = new UnaryMutator (UnaryMutator.Mode.PreDecrement,
3181 (Expression) $2, lexer.Location);
3185 multiplicative_expression
3186 : prefixed_unary_expression
3187 | multiplicative_expression STAR prefixed_unary_expression
3189 $$ = new Binary (Binary.Operator.Multiply,
3190 (Expression) $1, (Expression) $3, lexer.Location);
3192 | multiplicative_expression DIV prefixed_unary_expression
3194 $$ = new Binary (Binary.Operator.Division,
3195 (Expression) $1, (Expression) $3, lexer.Location);
3197 | multiplicative_expression PERCENT prefixed_unary_expression
3199 $$ = new Binary (Binary.Operator.Modulus,
3200 (Expression) $1, (Expression) $3, lexer.Location);
3205 : multiplicative_expression
3206 | additive_expression PLUS multiplicative_expression
3208 $$ = new Binary (Binary.Operator.Addition,
3209 (Expression) $1, (Expression) $3, lexer.Location);
3211 | additive_expression MINUS multiplicative_expression
3213 $$ = new Binary (Binary.Operator.Subtraction,
3214 (Expression) $1, (Expression) $3, lexer.Location);
3219 : additive_expression
3220 | shift_expression OP_SHIFT_LEFT additive_expression
3222 $$ = new Binary (Binary.Operator.LeftShift,
3223 (Expression) $1, (Expression) $3, lexer.Location);
3225 | shift_expression OP_SHIFT_RIGHT additive_expression
3227 $$ = new Binary (Binary.Operator.RightShift,
3228 (Expression) $1, (Expression) $3, lexer.Location);
3232 relational_expression
3234 | relational_expression OP_LT shift_expression
3236 $$ = new Binary (Binary.Operator.LessThan,
3237 (Expression) $1, (Expression) $3, lexer.Location);
3239 | relational_expression OP_GT shift_expression
3241 $$ = new Binary (Binary.Operator.GreaterThan,
3242 (Expression) $1, (Expression) $3, lexer.Location);
3244 | relational_expression OP_LE shift_expression
3246 $$ = new Binary (Binary.Operator.LessThanOrEqual,
3247 (Expression) $1, (Expression) $3, lexer.Location);
3249 | relational_expression OP_GE shift_expression
3251 $$ = new Binary (Binary.Operator.GreaterThanOrEqual,
3252 (Expression) $1, (Expression) $3, lexer.Location);
3254 | relational_expression IS type
3256 $$ = new Is ((Expression) $1, (Expression) $3, lexer.Location);
3258 | relational_expression AS type
3260 $$ = new As ((Expression) $1, (Expression) $3, lexer.Location);
3265 : relational_expression
3266 | equality_expression OP_EQ relational_expression
3268 $$ = new Binary (Binary.Operator.Equality,
3269 (Expression) $1, (Expression) $3, lexer.Location);
3271 | equality_expression OP_NE relational_expression
3273 $$ = new Binary (Binary.Operator.Inequality,
3274 (Expression) $1, (Expression) $3, lexer.Location);
3279 : equality_expression
3280 | and_expression BITWISE_AND equality_expression
3282 $$ = new Binary (Binary.Operator.BitwiseAnd,
3283 (Expression) $1, (Expression) $3, lexer.Location);
3287 exclusive_or_expression
3289 | exclusive_or_expression CARRET and_expression
3291 $$ = new Binary (Binary.Operator.ExclusiveOr,
3292 (Expression) $1, (Expression) $3, lexer.Location);
3296 inclusive_or_expression
3297 : exclusive_or_expression
3298 | inclusive_or_expression BITWISE_OR exclusive_or_expression
3300 $$ = new Binary (Binary.Operator.BitwiseOr,
3301 (Expression) $1, (Expression) $3, lexer.Location);
3305 conditional_and_expression
3306 : inclusive_or_expression
3307 | conditional_and_expression OP_AND inclusive_or_expression
3309 $$ = new Binary (Binary.Operator.LogicalAnd,
3310 (Expression) $1, (Expression) $3, lexer.Location);
3314 conditional_or_expression
3315 : conditional_and_expression
3316 | conditional_or_expression OP_OR conditional_and_expression
3318 $$ = new Binary (Binary.Operator.LogicalOr,
3319 (Expression) $1, (Expression) $3, lexer.Location);
3323 conditional_expression
3324 : conditional_or_expression
3325 | conditional_or_expression INTERR expression COLON expression
3327 $$ = new Conditional ((Expression) $1, (Expression) $3, (Expression) $5, lexer.Location);
3331 assignment_expression
3332 : prefixed_unary_expression ASSIGN expression
3334 $$ = new Assign ((Expression) $1, (Expression) $3, lexer.Location);
3336 | prefixed_unary_expression OP_MULT_ASSIGN expression
3338 Location l = lexer.Location;
3340 $$ = new CompoundAssign (
3341 Binary.Operator.Multiply, (Expression) $1, (Expression) $3, l);
3343 | prefixed_unary_expression OP_DIV_ASSIGN expression
3345 Location l = lexer.Location;
3347 $$ = new CompoundAssign (
3348 Binary.Operator.Division, (Expression) $1, (Expression) $3, l);
3350 | prefixed_unary_expression OP_MOD_ASSIGN expression
3352 Location l = lexer.Location;
3354 $$ = new CompoundAssign (
3355 Binary.Operator.Modulus, (Expression) $1, (Expression) $3, l);
3357 | prefixed_unary_expression OP_ADD_ASSIGN expression
3359 Location l = lexer.Location;
3361 $$ = new CompoundAssign (
3362 Binary.Operator.Addition, (Expression) $1, (Expression) $3, l);
3364 | prefixed_unary_expression OP_SUB_ASSIGN expression
3366 Location l = lexer.Location;
3368 $$ = new CompoundAssign (
3369 Binary.Operator.Subtraction, (Expression) $1, (Expression) $3, l);
3371 | prefixed_unary_expression OP_SHIFT_LEFT_ASSIGN expression
3373 Location l = lexer.Location;
3375 $$ = new CompoundAssign (
3376 Binary.Operator.LeftShift, (Expression) $1, (Expression) $3, l);
3378 | prefixed_unary_expression OP_SHIFT_RIGHT_ASSIGN expression
3380 Location l = lexer.Location;
3382 $$ = new CompoundAssign (
3383 Binary.Operator.RightShift, (Expression) $1, (Expression) $3, l);
3385 | prefixed_unary_expression OP_AND_ASSIGN expression
3387 Location l = lexer.Location;
3389 $$ = new CompoundAssign (
3390 Binary.Operator.BitwiseAnd, (Expression) $1, (Expression) $3, l);
3392 | prefixed_unary_expression OP_OR_ASSIGN expression
3394 Location l = lexer.Location;
3396 $$ = new CompoundAssign (
3397 Binary.Operator.BitwiseOr, (Expression) $1, (Expression) $3, l);
3399 | prefixed_unary_expression OP_XOR_ASSIGN expression
3401 Location l = lexer.Location;
3403 $$ = new CompoundAssign (
3404 Binary.Operator.ExclusiveOr, (Expression) $1, (Expression) $3, l);
3409 : conditional_expression
3410 | assignment_expression
3430 MemberName name = MakeName ((MemberName) $5);
3431 bool partial = (bool) $3;
3432 int mod_flags = (int) $2;
3435 ClassPart part = PartialContainer.CreatePart (
3436 current_namespace, current_class, name, mod_flags,
3437 (Attributes) $1, Kind.Class, lexer.Location);
3439 current_container = part.PartialContainer;
3440 current_class = part;
3442 if ((mod_flags & Modifiers.STATIC) != 0) {
3443 current_class = new StaticClass (
3444 current_namespace, current_class, name,
3445 mod_flags, (Attributes) $1, lexer.Location);
3447 current_class = new Class (
3448 current_namespace, current_class, name,
3449 mod_flags, (Attributes) $1, lexer.Location);
3452 current_container.AddClassOrStruct (current_class);
3453 current_container = current_class;
3454 RootContext.Tree.RecordDecl (current_namespace.NS, name, current_class);
3460 if (current_class.Name == "System.Object") {
3461 Report.Error (537, current_class.Location,
3462 "The class System.Object cannot have a base " +
3463 "class or implement an interface.");
3465 current_class.Bases = (ArrayList) $7;
3468 if (RootContext.Documentation != null) {
3469 current_class.DocComment = Lexer.consume_doc_comment ();
3470 Lexer.doc_state = XmlCommentState.Allowed;
3475 if (RootContext.Documentation != null)
3476 Lexer.doc_state = XmlCommentState.Allowed;
3480 $$ = pop_current_class ();
3486 { $$ = (bool) false; }
3488 { $$ = (bool) true; }
3492 : /* empty */ { $$ = (int) 0; }
3498 | modifiers modifier
3503 if ((m1 & m2) != 0) {
3504 Location l = lexer.Location;
3505 Report.Error (1004, l, "Duplicate `{0}' modifier", Modifiers.Name (m2));
3507 $$ = (int) (m1 | m2);
3512 : NEW { $$ = Modifiers.NEW; }
3513 | PUBLIC { $$ = Modifiers.PUBLIC; }
3514 | PROTECTED { $$ = Modifiers.PROTECTED; }
3515 | INTERNAL { $$ = Modifiers.INTERNAL; }
3516 | PRIVATE { $$ = Modifiers.PRIVATE; }
3517 | ABSTRACT { $$ = Modifiers.ABSTRACT; }
3518 | SEALED { $$ = Modifiers.SEALED; }
3519 | STATIC { $$ = Modifiers.STATIC; }
3520 | READONLY { $$ = Modifiers.READONLY; }
3521 | VIRTUAL { $$ = Modifiers.VIRTUAL; }
3522 | OVERRIDE { $$ = Modifiers.OVERRIDE; }
3523 | EXTERN { $$ = Modifiers.EXTERN; }
3524 | VOLATILE { $$ = Modifiers.VOLATILE; }
3525 | UNSAFE { $$ = Modifiers.UNSAFE; }
3529 : /* empty */ { $$ = null; }
3530 | class_base { $$ = $1; }
3534 : COLON type_list { $$ = $2; }
3542 // A block is "contained" on the following places:
3544 // property_declaration as part of the accessor body (get/set)
3545 // operator_declaration
3546 // constructor_declaration
3547 // destructor_declaration
3548 // event_declaration as part of add_accessor_declaration or remove_accessor_declaration
3553 if (current_block == null){
3554 current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
3555 top_current_block = current_block;
3557 current_block = new Block (current_block, lexer.Location, Location.Null);
3560 opt_statement_list CLOSE_BRACE
3562 while (current_block.Implicit)
3563 current_block = current_block.Parent;
3565 current_block.SetEndLocation (lexer.Location);
3566 current_block = current_block.Parent;
3567 if (current_block == null)
3568 top_current_block = null;
3579 | statement_list statement
3583 : declaration_statement
3585 if ($1 != null && (Block) $1 != current_block){
3586 current_block.AddStatement ((Statement) $1);
3587 current_block = (Block) $1;
3590 | valid_declaration_statement
3592 current_block.AddStatement ((Statement) $1);
3597 valid_declaration_statement
3600 | expression_statement
3601 | selection_statement
3602 | iteration_statement
3606 | unchecked_statement
3614 : valid_declaration_statement
3615 | declaration_statement
3617 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration or labeled statement");
3622 Report.Error (1023, lexer.Location, "An embedded statement may not be a declaration or labeled statement");
3630 $$ = EmptyStatement.Value;
3637 LabeledStatement labeled = new LabeledStatement ((string) $1, lexer.Location);
3639 if (current_block.AddLabel ((string) $1, labeled, lexer.Location))
3640 current_block.AddStatement (labeled);
3645 declaration_statement
3646 : local_variable_declaration SEMICOLON
3649 DictionaryEntry de = (DictionaryEntry) $1;
3651 $$ = declare_local_variables ((Expression) de.Key, (ArrayList) de.Value, lexer.Location);
3655 | local_constant_declaration SEMICOLON
3658 DictionaryEntry de = (DictionaryEntry) $1;
3660 $$ = declare_local_constants ((Expression) de.Key, (ArrayList) de.Value);
3666 * The following is from Rhys' grammar:
3667 * > Types in local variable declarations must be recognized as
3668 * > expressions to prevent reduce/reduce errors in the grammar.
3669 * > The expressions are converted into types during semantic analysis.
3672 : primary_expression opt_rank_specifier
3674 // FIXME: Do something smart here regarding the composition of the type.
3676 // Ok, the above "primary_expression" is there to get rid of
3677 // both reduce/reduce and shift/reduces in the grammar, it should
3678 // really just be "type_name". If you use type_name, a reduce/reduce
3679 // creeps up. If you use namespace_or_type_name (which is all we need
3680 // really) two shift/reduces appear.
3683 // So the super-trick is that primary_expression
3684 // can only be either a SimpleName or a MemberAccess.
3685 // The MemberAccess case arises when you have a fully qualified type-name like :
3687 // SimpleName is when you have
3690 Expression expr = (Expression) $1;
3691 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3692 Error_ExpectingTypeName (lexer.Location, expr);
3696 // So we extract the string corresponding to the SimpleName
3700 if ((string) $2 == "")
3703 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3706 | builtin_types opt_rank_specifier
3708 if ((string) $2 == "")
3711 $$ = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3715 local_variable_pointer_type
3716 : primary_expression STAR
3718 Expression expr = (Expression) $1;
3719 Location l = lexer.Location;
3721 if (!(expr is SimpleName || expr is MemberAccess || expr is ComposedCast)) {
3722 Error_ExpectingTypeName (l, expr);
3726 $$ = new ComposedCast ((Expression) $1, "*", l);
3728 | builtin_types STAR
3730 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);;
3734 $$ = new ComposedCast (TypeManager.system_void_expr, "*", lexer.Location);;
3736 | local_variable_pointer_type STAR
3738 $$ = new ComposedCast ((Expression) $1, "*", lexer.Location);
3742 local_variable_declaration
3743 : local_variable_type variable_declarators
3746 $$ = new DictionaryEntry ($1, $2);
3750 | local_variable_pointer_type opt_rank_specifier variable_declarators
3755 if ((string) $2 == "")
3756 t = (Expression) $1;
3758 t = new ComposedCast ((Expression) $1, (string) $2, lexer.Location);
3759 $$ = new DictionaryEntry (t, $3);
3765 local_constant_declaration
3766 : CONST local_variable_type constant_declarators
3769 $$ = new DictionaryEntry ($2, $3);
3775 expression_statement
3776 : statement_expression SEMICOLON
3783 // We have to do the wrapping here and not in the case above,
3784 // because statement_expression is used for example in for_statement
3786 statement_expression
3787 : invocation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3788 | object_creation_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3789 | assignment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3790 | post_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3791 | post_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3792 | pre_increment_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3793 | pre_decrement_expression { $$ = new StatementExpression ((ExpressionStatement) $1, lexer.Location); }
3795 Report.Error (1002, lexer.Location, "Expecting `;'");
3800 object_creation_expression
3801 : object_or_delegate_creation_expression
3802 { note ("complain if this is a delegate maybe?"); }
3811 : IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3814 Location l = (Location) $3;
3816 $$ = new If ((Expression) $4, (Statement) $6, l);
3818 if (RootContext.WarningLevel >= 4){
3819 if ($6 == EmptyStatement.Value)
3820 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3824 | IF OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS
3825 embedded_statement ELSE embedded_statement
3827 Location l = (Location) $3;
3829 $$ = new If ((Expression) $4, (Statement) $6, (Statement) $8, l);
3834 : SWITCH OPEN_PARENS _mark_
3836 switch_stack.Push (current_block);
3838 expression CLOSE_PARENS
3841 $$ = new Switch ((Expression) $5, (ArrayList) $7, (Location) $3);
3842 current_block = (Block) switch_stack.Pop ();
3858 Report.Error (1522, lexer.Location, "Empty switch block");
3866 ArrayList sections = new ArrayList (4);
3871 | switch_sections switch_section
3873 ArrayList sections = (ArrayList) $1;
3883 current_block = current_block.CreateSwitchBlock (lexer.Location);
3887 Block topmost = current_block;
3889 while (topmost.Implicit)
3890 topmost = topmost.Parent;
3891 $$ = new SwitchSection ((ArrayList) $1, topmost);
3898 ArrayList labels = new ArrayList (4);
3903 | switch_labels switch_label
3905 ArrayList labels = (ArrayList) ($1);
3913 : CASE constant_expression COLON { $$ = new SwitchLabel ((Expression) $2, lexer.Location); }
3914 | DEFAULT COLON { $$ = new SwitchLabel (null, lexer.Location); }
3917 1523, lexer.Location,
3918 "The keyword case or default must precede code in switch block");
3930 : WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS embedded_statement
3932 Location l = (Location) $3;
3933 $$ = new While ((Expression) $4, (Statement) $6, l);
3935 if (RootContext.WarningLevel >= 4){
3936 if ($6 == EmptyStatement.Value)
3937 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
3943 : DO embedded_statement
3944 WHILE OPEN_PARENS _mark_ boolean_expression CLOSE_PARENS SEMICOLON
3946 Location l = (Location) $5;
3948 $$ = new Do ((Statement) $2, (Expression) $6, l);
3954 opt_for_initializer SEMICOLON _mark_
3956 Block assign_block = new Block (current_block);
3957 current_block = assign_block;
3959 if ($3 is DictionaryEntry){
3960 DictionaryEntry de = (DictionaryEntry) $3;
3962 Expression type = (Expression) de.Key;
3963 ArrayList var_declarators = (ArrayList) de.Value;
3965 foreach (VariableDeclaration decl in var_declarators){
3969 vi = current_block.AddVariable (type, decl.identifier, decl.Location);
3973 Location l = lexer.Location;
3975 if (decl.expression_or_array_initializer is Expression){
3976 expr = (Expression) decl.expression_or_array_initializer;
3977 } else if (decl.expression_or_array_initializer == null) {
3980 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
3981 expr = new ArrayCreation (type, "", init, decl.Location);
3984 LocalVariableReference var;
3985 var = new LocalVariableReference (assign_block, decl.identifier, l);
3988 Assign a = new Assign (var, expr, decl.Location);
3990 assign_block.AddStatement (new StatementExpression (a, lexer.Location));
3994 // Note: the $$ below refers to the value of this code block, not of the LHS non-terminal.
3995 // This can be referred to as $6 below.
4001 opt_for_condition SEMICOLON
4002 opt_for_iterator CLOSE_PARENS
4005 Location l = (Location) $5;
4007 For f = new For ((Statement) $6, (Expression) $7, (Statement) $9, (Statement) $11, l);
4009 if (RootContext.WarningLevel >= 4){
4010 if ($11 == EmptyStatement.Value)
4011 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4014 current_block.AddStatement (f);
4015 while (current_block.Implicit)
4016 current_block = current_block.Parent;
4018 current_block = current_block.Parent;
4023 : /* empty */ { $$ = EmptyStatement.Value; }
4028 : local_variable_declaration
4029 | statement_expression_list
4033 : /* empty */ { $$ = null; }
4034 | boolean_expression
4038 : /* empty */ { $$ = EmptyStatement.Value; }
4043 : statement_expression_list
4046 statement_expression_list
4047 : statement_expression
4049 // CHANGE: was `null'
4050 Block b = new Block (current_block, Block.Flags.Implicit);
4052 b.AddStatement ((Statement) $1);
4055 | statement_expression_list COMMA statement_expression
4057 Block b = (Block) $1;
4059 b.AddStatement ((Statement) $3);
4065 : FOREACH OPEN_PARENS type IN expression CLOSE_PARENS
4067 Report.Error (230, lexer.Location, "Type and identifier are both required in a foreach statement");
4070 | FOREACH OPEN_PARENS type IDENTIFIER IN _mark_
4071 expression CLOSE_PARENS
4073 Block foreach_block = new Block (current_block);
4074 current_block = foreach_block;
4076 Location l = lexer.Location;
4079 vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
4081 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Foreach);
4083 // Get a writable reference to this read-only variable.
4085 // Note that the $$ here refers to the value of _this_ code block,
4086 // not the value of the LHS non-terminal. This can be referred to as $9 below.
4087 $$ = new LocalVariableReference (foreach_block, (string) $4, l, vi, false);
4094 LocalVariableReference v = (LocalVariableReference) $9;
4095 Location l = (Location) $6;
4098 Foreach f = new Foreach ((Expression) $3, v, (Expression) $7, (Statement) $10, l);
4099 current_block.AddStatement (f);
4102 while (current_block.Implicit)
4103 current_block = current_block.Parent;
4105 current_block = current_block.Parent;
4111 | continue_statement
4121 $$ = new Break (lexer.Location);
4126 : CONTINUE SEMICOLON
4128 $$ = new Continue (lexer.Location);
4133 : GOTO IDENTIFIER SEMICOLON
4135 $$ = new Goto ((string) $2, lexer.Location);
4137 | GOTO CASE constant_expression SEMICOLON
4139 $$ = new GotoCase ((Expression) $3, lexer.Location);
4141 | GOTO DEFAULT SEMICOLON
4143 $$ = new GotoDefault (lexer.Location);
4148 : RETURN opt_expression SEMICOLON
4150 $$ = new Return ((Expression) $2, lexer.Location);
4155 : THROW opt_expression SEMICOLON
4157 $$ = new Throw ((Expression) $2, lexer.Location);
4162 : IDENTIFIER RETURN expression SEMICOLON
4164 string s = (string) $1;
4166 Report.Error (1003, lexer.Location, "; expected");
4169 if (RootContext.Version == LanguageVersion.ISO_1){
4170 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4173 if (iterator_container == null){
4174 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4177 iterator_container.SetYields ();
4178 $$ = new Yield ((Expression) $3, lexer.Location);
4181 | IDENTIFIER RETURN SEMICOLON
4183 Report.Error (1627, lexer.Location, "Expression expected after yield return");
4186 | IDENTIFIER BREAK SEMICOLON
4188 string s = (string) $1;
4190 Report.Error (1003, lexer.Location, "; expected");
4193 if (RootContext.Version == LanguageVersion.ISO_1){
4194 Report.FeatureIsNotStandardized (lexer.Location, "yield statement");
4197 if (iterator_container == null){
4198 Report.Error (204, lexer.Location, "yield statement can only be used within a method, operator or property");
4201 iterator_container.SetYields ();
4202 $$ = new YieldBreak (lexer.Location);
4213 : TRY block catch_clauses
4217 ArrayList c = (ArrayList)$3;
4218 for (int i = 0; i < c.Count; ++i) {
4219 Catch cc = (Catch) c [i];
4221 if (i != c.Count - 1)
4222 Report.Error (1017, cc.loc, "Try statement already has an empty catch block");
4229 // Now s contains the list of specific catch clauses
4230 // and g contains the general one.
4232 $$ = new Try ((Block) $2, c, g, null, ((Block) $2).loc);
4234 | TRY block opt_catch_clauses FINALLY block
4237 ArrayList s = new ArrayList (4);
4238 ArrayList catch_list = (ArrayList) $3;
4240 if (catch_list != null){
4241 foreach (Catch cc in catch_list) {
4249 $$ = new Try ((Block) $2, s, g, (Block) $5, ((Block) $2).loc);
4253 Report.Error (1524, lexer.Location, "Expected catch or finally");
4258 : /* empty */ { $$ = null; }
4265 ArrayList l = new ArrayList (4);
4270 | catch_clauses catch_clause
4272 ArrayList l = (ArrayList) $1;
4280 : /* empty */ { $$ = null; }
4285 : CATCH opt_catch_args
4287 Expression type = null;
4291 DictionaryEntry cc = (DictionaryEntry) $2;
4292 type = (Expression) cc.Key;
4293 id = (string) cc.Value;
4296 ArrayList one = new ArrayList (4);
4297 Location loc = lexer.Location;
4299 one.Add (new VariableDeclaration (id, null, loc));
4301 current_block = new Block (current_block);
4302 Block b = declare_local_variables (type, one, loc);
4307 Expression type = null;
4311 DictionaryEntry cc = (DictionaryEntry) $2;
4312 type = (Expression) cc.Key;
4313 id = (string) cc.Value;
4316 while (current_block.Implicit)
4317 current_block = current_block.Parent;
4318 current_block = current_block.Parent;
4322 $$ = new Catch (type, id, (Block) $4, ((Block) $4).loc);
4327 : /* empty */ { $$ = null; }
4332 : OPEN_PARENS type opt_identifier CLOSE_PARENS
4334 $$ = new DictionaryEntry ($2, $3);
4341 $$ = new Checked ((Block) $2);
4348 $$ = new Unchecked ((Block) $2);
4355 RootContext.CheckUnsafeOption (lexer.Location);
4357 $$ = new Unsafe ((Block) $3);
4363 type fixed_pointer_declarators
4366 ArrayList list = (ArrayList) $4;
4367 Expression type = (Expression) $3;
4368 Location l = lexer.Location;
4369 int top = list.Count;
4371 Block assign_block = new Block (current_block);
4372 current_block = assign_block;
4374 for (int i = 0; i < top; i++){
4375 Pair p = (Pair) list [i];
4378 v = current_block.AddVariable (type, (string) p.First, l);
4382 v.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
4390 Location l = (Location) $6;
4392 Fixed f = new Fixed ((Expression) $3, (ArrayList) $4, (Statement) $8, l);
4394 if (RootContext.WarningLevel >= 4){
4395 if ($8 == EmptyStatement.Value)
4396 Report.Warning (642, lexer.Location, "Possible mistaken empty statement");
4399 current_block.AddStatement (f);
4400 while (current_block.Implicit)
4401 current_block = current_block.Parent;
4403 current_block = current_block.Parent;
4407 fixed_pointer_declarators
4408 : fixed_pointer_declarator {
4409 ArrayList declarators = new ArrayList (4);
4411 declarators.Add ($1);
4414 | fixed_pointer_declarators COMMA fixed_pointer_declarator
4416 ArrayList declarators = (ArrayList) $1;
4418 declarators.Add ($3);
4423 fixed_pointer_declarator
4424 : IDENTIFIER ASSIGN expression
4426 $$ = new Pair ($1, $3);
4430 Report.Error (210, lexer.Location, "You must provide an initializer in a fixed or using statement declaration");
4436 : LOCK OPEN_PARENS expression CLOSE_PARENS
4442 $$ = new Lock ((Expression) $3, (Statement) $6, lexer.Location);
4447 : USING OPEN_PARENS resource_acquisition CLOSE_PARENS _mark_
4449 Block assign_block = new Block (current_block);
4450 current_block = assign_block;
4452 if ($3 is DictionaryEntry){
4453 DictionaryEntry de = (DictionaryEntry) $3;
4454 Location l = lexer.Location;
4456 Expression type = (Expression) de.Key;
4457 ArrayList var_declarators = (ArrayList) de.Value;
4459 ArrayList vars = new ArrayList (4);
4461 foreach (VariableDeclaration decl in var_declarators){
4463 LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
4466 vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Using);
4469 if (decl.expression_or_array_initializer is Expression){
4470 expr = (Expression) decl.expression_or_array_initializer;
4472 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4474 Report.Error (210, l, "You must provide an initializer in a fixed or using statement declaration");
4477 expr = new ArrayCreation (type, "", init, decl.Location);
4480 LocalVariableReference var;
4482 // Get a writable reference to this read-only variable.
4483 var = new LocalVariableReference (assign_block, decl.identifier, l, vi, false);
4485 // This is so that it is not a warning on using variables
4488 vars.Add (new DictionaryEntry (var, expr));
4490 // Assign a = new Assign (var, expr, decl.Location);
4491 // assign_block.AddStatement (new StatementExpression (a, lexer.Location));
4494 // Note: the $$ here refers to the value of this code block and not of the LHS non-terminal.
4495 // It can be referred to as $6 below.
4496 $$ = new DictionaryEntry (type, vars);
4503 Using u = new Using ($6, (Statement) $7, (Location) $5);
4504 current_block.AddStatement (u);
4505 while (current_block.Implicit)
4506 current_block = current_block.Parent;
4508 current_block = current_block.Parent;
4512 resource_acquisition
4513 : local_variable_declaration
4517 // Utility rule to save location information
4520 { $$ = lexer.Location; }
4525 // A class used to pass around variable declarations and constants
4527 public class VariableDeclaration {
4528 public string identifier;
4529 public object expression_or_array_initializer;
4530 public Location Location;
4531 public Attributes OptAttributes;
4532 public string DocComment;
4534 public VariableDeclaration (string id, object eoai, Location l, Attributes opt_attrs)
4536 this.identifier = id;
4537 this.expression_or_array_initializer = eoai;
4539 this.OptAttributes = opt_attrs;
4542 public VariableDeclaration (string id, object eoai, Location l) : this (id, eoai, l, null)
4548 /// Used to pass around interface property information
4550 public class InterfaceAccessorInfo {
4552 public readonly Accessor Get, Set;
4554 public InterfaceAccessorInfo (bool has_get, bool has_set,
4555 Attributes get_attrs, Attributes set_attrs, int get_mod, int set_mod, Location get_loc, Location set_loc)
4558 Get = new Accessor (null, get_mod, get_attrs, get_loc);
4560 Set = new Accessor (null, set_mod, set_attrs, set_loc);
4567 // A class used to hold info about an indexer declarator
4569 public class IndexerDeclaration {
4570 public Expression type;
4571 public MemberName interface_type;
4572 public Parameters param_list;
4574 public IndexerDeclaration (Expression type, MemberName interface_type,
4575 Parameters param_list)
4578 this.interface_type = interface_type;
4579 this.param_list = param_list;
4584 // We use this when we do not have an object in advance that is an IIteratorContainer
4586 public class SimpleIteratorContainer : IIteratorContainer {
4589 public static SimpleIteratorContainer Simple = new SimpleIteratorContainer ();
4594 public static SimpleIteratorContainer GetSimple () {
4595 Simple.Yields = false;
4599 public void SetYields () { Yields = true; }
4603 // A class used to hold info about an operator declarator
4605 public class OperatorDeclaration {
4606 public Operator.OpType optype;
4607 public Expression ret_type, arg1type, arg2type;
4608 public string arg1name, arg2name;
4609 public Location location;
4611 public OperatorDeclaration (Operator.OpType op, Expression ret_type,
4612 Expression arg1type, string arg1name,
4613 Expression arg2type, string arg2name, Location location)
4616 this.ret_type = ret_type;
4617 this.arg1type = arg1type;
4618 this.arg1name = arg1name;
4619 this.arg2type = arg2type;
4620 this.arg2name = arg2name;
4621 this.location = location;
4626 void Error_ExpectingTypeName (Location l, Expression expr)
4628 if (expr is Invocation){
4629 Report.Error (1002, l, "Expecting `;'");
4631 Report.Error (-1, l, "Invalid Type definition");
4635 TypeContainer pop_current_class ()
4637 TypeContainer retval = current_class;
4639 current_class = current_class.Parent;
4640 current_container = current_container.Parent;
4642 if (current_class != current_container) {
4643 if (!(current_class is ClassPart) ||
4644 ((ClassPart) current_class).PartialContainer != current_container)
4645 throw new InternalErrorException ();
4646 } else if (current_container is ClassPart)
4647 current_container = ((ClassPart) current_class).PartialContainer;
4653 // Given the @class_name name, it creates a fully qualified name
4654 // based on the containing declaration space
4657 MakeName (MemberName class_name)
4659 Namespace ns = current_namespace.NS;
4661 if (current_container.Name == ""){
4663 return new MemberName (ns.MemberName, class_name);
4667 return new MemberName (current_container.MemberName, class_name);
4671 Block declare_local_variables (Expression type, ArrayList variable_declarators, Location loc)
4673 Block implicit_block;
4674 ArrayList inits = null;
4677 // We use the `Used' property to check whether statements
4678 // have been added to the current block. If so, we need
4679 // to create another block to contain the new declaration
4680 // otherwise, as an optimization, we use the same block to
4681 // add the declaration.
4683 // FIXME: A further optimization is to check if the statements
4684 // that were added were added as part of the initialization
4685 // below. In which case, no other statements have been executed
4686 // and we might be able to reduce the number of blocks for
4687 // situations like this:
4689 // int j = 1; int k = j + 1;
4691 if (current_block.Used)
4692 implicit_block = new Block (current_block, Block.Flags.Implicit, loc, Location.Null);
4694 implicit_block = current_block;
4696 foreach (VariableDeclaration decl in variable_declarators){
4698 if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
4699 if (decl.expression_or_array_initializer != null){
4701 inits = new ArrayList (4);
4708 return implicit_block;
4710 foreach (VariableDeclaration decl in inits){
4714 if (decl.expression_or_array_initializer is Expression){
4715 expr = (Expression) decl.expression_or_array_initializer;
4718 ArrayList init = (ArrayList) decl.expression_or_array_initializer;
4720 expr = new ArrayCreation (type, "", init, decl.Location);
4723 LocalVariableReference var;
4724 var = new LocalVariableReference (implicit_block, decl.identifier, loc);
4726 assign = new Assign (var, expr, decl.Location);
4728 implicit_block.AddStatement (new StatementExpression (assign, lexer.Location));
4731 return implicit_block;
4734 Block declare_local_constants (Expression type, ArrayList declarators)
4736 Block implicit_block;
4738 if (current_block.Used)
4739 implicit_block = new Block (current_block, Block.Flags.Implicit);
4741 implicit_block = current_block;
4743 foreach (VariableDeclaration decl in declarators){
4744 implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
4747 return implicit_block;
4750 void CheckAttributeTarget (string a)
4754 case "assembly" : case "module" : case "field" : case "method" : case "param" : case "property" : case "type" :
4758 Location l = lexer.Location;
4759 Report.Error (658, l, "`" + a + "' is an invalid attribute target");
4765 void CheckUnaryOperator (Operator.OpType op)
4769 case Operator.OpType.LogicalNot:
4770 case Operator.OpType.OnesComplement:
4771 case Operator.OpType.Increment:
4772 case Operator.OpType.Decrement:
4773 case Operator.OpType.True:
4774 case Operator.OpType.False:
4775 case Operator.OpType.Addition:
4776 case Operator.OpType.Subtraction:
4781 Location l = lexer.Location;
4782 Report.Error (1019, l, "Overloadable unary operator expected");
4788 void CheckBinaryOperator (Operator.OpType op)
4792 case Operator.OpType.Addition:
4793 case Operator.OpType.Subtraction:
4794 case Operator.OpType.Multiply:
4795 case Operator.OpType.Division:
4796 case Operator.OpType.Modulus:
4797 case Operator.OpType.BitwiseAnd:
4798 case Operator.OpType.BitwiseOr:
4799 case Operator.OpType.ExclusiveOr:
4800 case Operator.OpType.LeftShift:
4801 case Operator.OpType.RightShift:
4802 case Operator.OpType.Equality:
4803 case Operator.OpType.Inequality:
4804 case Operator.OpType.GreaterThan:
4805 case Operator.OpType.LessThan:
4806 case Operator.OpType.GreaterThanOrEqual:
4807 case Operator.OpType.LessThanOrEqual:
4811 Location l = lexer.Location;
4812 Report.Error (1020, l, "Overloadable binary operator expected");
4818 void syntax_error (Location l, string msg)
4820 Report.Error (1003, l, "Syntax error, " + msg);
4823 void note (string s)
4825 // Used to put annotations
4830 public Tokenizer Lexer {
4836 public CSharpParser (SeekableStreamReader reader, SourceFile file, ArrayList defines)
4838 current_namespace = new NamespaceEntry (null, file, null, Location.Null);
4839 this.name = file.Name;
4841 current_container = RootContext.Tree.Types;
4842 // TODO: Make RootContext.Tree.Types a PartialContainer.
4843 current_class = current_container;
4844 current_container.NamespaceEntry = current_namespace;
4845 oob_stack = new Stack ();
4846 switch_stack = new Stack ();
4848 lexer = new Tokenizer (reader, file, defines);
4851 public void parse ()
4854 if (yacc_verbose_flag > 1)
4855 yyparse (lexer, new yydebug.yyDebugSimple ());
4858 Tokenizer tokenizer = lexer as Tokenizer;
4859 tokenizer.cleanup ();
4860 } catch (Exception e){
4862 // Removed for production use, use parser verbose to get the output.
4864 // Console.WriteLine (e);
4865 Report.Error (-25, lexer.Location, "Parsing error");
4866 if (yacc_verbose_flag > 0)
4867 Console.WriteLine (e);
4870 RootContext.Tree.Types.NamespaceEntry = null;
4873 void CheckToken (int error, int yyToken, string msg)
4875 if (yyToken >= Token.FIRST_KEYWORD && yyToken <= Token.LAST_KEYWORD){
4876 Report.Error (error, lexer.Location, String.Format ("{0}: `{1}' is a keyword", msg, yyNames [yyToken].ToLower ()));
4879 Report.Error (error, lexer.Location, msg);
4882 void CheckIdentifierToken (int yyToken)
4884 CheckToken (1041, yyToken, "Identifier expected");
4887 string ConsumeStoredComment ()
4889 string s = tmpComment;
4891 Lexer.doc_state = XmlCommentState.Allowed;