Changeset 777ed2b for src/Parser
- Timestamp:
- Jul 11, 2018, 11:55:59 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- 0fc52b6
- Parents:
- fc20514 (diff), 7de22b28 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/Parser
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
rfc20514 r777ed2b 254 254 } // DeclarationNode::newFromTypedef 255 255 256 DeclarationNode * DeclarationNode::newFromGlobalScope() { 257 DeclarationNode * newnode = new DeclarationNode; 258 newnode->type = new TypeData( TypeData::GlobalScope ); 259 return newnode; 260 } 261 262 DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) { 263 DeclarationNode * newnode = new DeclarationNode; 264 newnode->type = new TypeData( TypeData::Qualified ); 265 newnode->type->qualified.parent = parent->type; 266 newnode->type->qualified.child = child->type; 267 parent->type = nullptr; 268 child->type = nullptr; 269 delete parent; 270 delete child; 271 return newnode; 272 } 273 256 274 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { 257 assert( name );258 275 DeclarationNode * newnode = new DeclarationNode; 259 276 newnode->type = new TypeData( TypeData::Aggregate ); 260 277 newnode->type->aggregate.kind = kind; 261 newnode->type->aggregate.name = name;278 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name; 262 279 newnode->type->aggregate.actuals = actuals; 263 280 newnode->type->aggregate.fields = fields; … … 265 282 newnode->type->aggregate.tagged = false; 266 283 newnode->type->aggregate.parent = nullptr; 284 newnode->type->aggregate.anon = name == nullptr; 267 285 return newnode; 268 286 } // DeclarationNode::newAggregate 269 287 270 288 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) { 271 assert( name );272 289 DeclarationNode * newnode = new DeclarationNode; 273 290 newnode->type = new TypeData( TypeData::Enum ); 274 newnode->type->enumeration.name = name ;291 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name; 275 292 newnode->type->enumeration.constants = constants; 276 293 newnode->type->enumeration.body = body; 294 newnode->type->enumeration.anon = name == nullptr; 277 295 return newnode; 278 296 } // DeclarationNode::newEnum … … 953 971 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 954 972 try { 973 bool extracted = false; 974 bool anon = false; 955 975 if ( DeclarationNode * extr = cur->extractAggregate() ) { 956 976 // handle the case where a structure declaration is contained within an object or type declaration 957 977 Declaration * decl = extr->build(); 958 978 if ( decl ) { 979 // hoist the structure declaration 959 980 decl->location = cur->location; 960 981 * out++ = decl; 982 983 // need to remember the cases where a declaration contains an anonymous aggregate definition 984 extracted = true; 985 assert( extr->type ); 986 if ( extr->type->kind == TypeData::Aggregate ) { 987 anon = extr->type->aggregate.anon; 988 } else if ( extr->type->kind == TypeData::Enum ) { 989 // xxx - is it useful to have an implicit anonymous enum member? 990 anon = extr->type->enumeration.anon; 991 } 961 992 } // if 962 993 delete extr; … … 965 996 Declaration * decl = cur->build(); 966 997 if ( decl ) { 967 decl->location = cur->location; 968 * out++ = decl; 998 // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.: 999 // struct S { 1000 // struct T { int x; }; // no anonymous member 1001 // struct { int y; }; // anonymous member 1002 // struct T; // anonymous member 1003 // }; 1004 if ( ! (extracted && decl->name == "" && ! anon) ) { 1005 decl->location = cur->location; 1006 * out++ = decl; 1007 } 969 1008 } // if 970 1009 } catch( SemanticErrorException &e ) { … … 978 1017 } // buildList 979 1018 1019 // currently only builds assertions, function parameters, and return values 980 1020 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 981 1021 SemanticErrorException errors; … … 985 1025 try { 986 1026 Declaration * decl = cur->build(); 987 if ( decl ) { 988 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 989 dwt->location = cur->location; 990 * out++ = dwt; 991 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 992 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 993 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 994 obj->location = cur->location; 995 * out++ = obj; 996 delete agg; 997 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 998 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 999 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1000 obj->location = cur->location; 1001 * out++ = obj; 1002 } // if 1027 assert( decl ); 1028 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 1029 dwt->location = cur->location; 1030 * out++ = dwt; 1031 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 1032 // e.g., int foo(struct S) {} 1033 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name ); 1034 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1035 obj->location = cur->location; 1036 * out++ = obj; 1037 delete agg; 1038 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 1039 // e.g., int foo(union U) {} 1040 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name ); 1041 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1042 obj->location = cur->location; 1043 * out++ = obj; 1044 } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) { 1045 // e.g., int foo(enum E) {} 1046 EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name ); 1047 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1048 obj->location = cur->location; 1049 * out++ = obj; 1003 1050 } // if 1004 1051 } catch( SemanticErrorException &e ) { -
src/Parser/ParseNode.h
rfc20514 r777ed2b 231 231 static DeclarationNode * newForall( DeclarationNode * ); 232 232 static DeclarationNode * newFromTypedef( const std::string * ); 233 static DeclarationNode * newFromGlobalScope(); 234 static DeclarationNode * newQualifiedType( DeclarationNode *, DeclarationNode * ); 233 235 static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ); 234 236 static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ); -
src/Parser/TypeData.cc
rfc20514 r777ed2b 37 37 case Reference: 38 38 case EnumConstant: 39 case GlobalScope: 39 40 // nothing else to initialize 40 41 break; … … 62 63 enumeration.constants = nullptr; 63 64 enumeration.body = false; 65 enumeration.anon = false; 64 66 break; 65 67 case Aggregate: … … 73 75 aggregate.tagged = false; 74 76 aggregate.parent = nullptr; 77 aggregate.anon = false; 75 78 break; 76 79 case AggregateInst: … … 98 101 case Builtin: 99 102 // builtin = new Builtin_t; 103 case Qualified: 104 qualified.parent = nullptr; 105 qualified.child = nullptr; 100 106 break; 101 107 } // switch … … 112 118 case Reference: 113 119 case EnumConstant: 120 case GlobalScope: 114 121 // nothing to destroy 115 122 break; … … 165 172 // delete builtin; 166 173 break; 174 case Qualified: 175 delete qualified.parent; 176 delete qualified.child; 167 177 } // switch 168 178 } // TypeData::~TypeData … … 180 190 case Pointer: 181 191 case Reference: 192 case GlobalScope: 182 193 // nothing else to copy 183 194 break; … … 207 218 newtype->aggregate.fields = maybeClone( aggregate.fields ); 208 219 newtype->aggregate.body = aggregate.body; 220 newtype->aggregate.anon = aggregate.anon; 209 221 newtype->aggregate.tagged = aggregate.tagged; 210 222 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr; … … 219 231 newtype->enumeration.constants = maybeClone( enumeration.constants ); 220 232 newtype->enumeration.body = enumeration.body; 233 newtype->enumeration.anon = enumeration.anon; 221 234 break; 222 235 case Symbolic: … … 238 251 newtype->builtintype = builtintype; 239 252 break; 253 case Qualified: 254 newtype->qualified.parent = maybeClone( qualified.parent ); 255 newtype->qualified.child = maybeClone( qualified.child ); 256 break; 240 257 } // switch 241 258 return newtype; … … 406 423 } // switch 407 424 } // TypeData::print 425 426 const std::string * TypeData::leafName() const { 427 switch ( kind ) { 428 case Unknown: 429 case Pointer: 430 case Reference: 431 case EnumConstant: 432 case GlobalScope: 433 case Array: 434 case Basic: 435 case Function: 436 case AggregateInst: 437 case Tuple: 438 case Typeof: 439 case Builtin: 440 assertf(false, "Tried to get leaf name from kind without a name: %d", kind); 441 break; 442 case Aggregate: 443 return aggregate.name; 444 case Enum: 445 return enumeration.name; 446 case Symbolic: 447 case SymbolicInst: 448 return symbolic.name; 449 case Qualified: 450 return qualified.child->leafName(); 451 } // switch 452 assert(false); 453 } 408 454 409 455 … … 465 511 return new EnumInstType( buildQualifiers( td ), "" ); 466 512 case TypeData::SymbolicInst: 467 return buildSymbolicInst( td ); ;513 return buildSymbolicInst( td ); 468 514 case TypeData::Tuple: 469 515 return buildTuple( td ); … … 480 526 return new VarArgsType( buildQualifiers( td ) ); 481 527 } 528 case TypeData::GlobalScope: 529 return new GlobalScopeType(); 530 case TypeData::Qualified: 531 return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) ); 482 532 case TypeData::Symbolic: 483 533 case TypeData::Enum: … … 485 535 assert( false ); 486 536 } // switch 537 487 538 return nullptr; 488 539 } // typebuild … … 893 944 assert( td->kind == TypeData::Function ); 894 945 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis ); 895 buildList( td->function.params, ft-> get_parameters());896 buildForall( td->forall, ft-> get_forall());946 buildList( td->function.params, ft->parameters ); 947 buildForall( td->forall, ft->forall ); 897 948 if ( td->base ) { 898 949 switch ( td->base->kind ) { 899 950 case TypeData::Tuple: 900 buildList( td->base->tuple, ft-> get_returnVals());951 buildList( td->base->tuple, ft->returnVals ); 901 952 break; 902 953 default: -
src/Parser/TypeData.h
rfc20514 r777ed2b 27 27 struct TypeData { 28 28 enum Kind { Basic, Pointer, Array, Reference, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic, 29 SymbolicInst, Tuple, Typeof, Builtin, Unknown };29 SymbolicInst, Tuple, Typeof, Builtin, GlobalScope, Qualified, Unknown }; 30 30 31 31 struct Aggregate_t { … … 36 36 DeclarationNode * fields; 37 37 bool body; 38 bool anon; 38 39 39 40 bool tagged; … … 57 58 DeclarationNode * constants; 58 59 bool body; 60 bool anon; 59 61 }; 60 62 … … 75 77 }; 76 78 79 struct Qualified_t { // qualified type S.T 80 TypeData * parent; 81 TypeData * child; 82 }; 83 77 84 CodeLocation location; 78 85 … … 88 95 DeclarationNode * forall; 89 96 90 // Basic_t basic;91 97 Aggregate_t aggregate; 92 98 AggInst_t aggInst; 93 99 Array_t array; 94 100 Enumeration_t enumeration; 95 // Variable_t variable;96 101 Function_t function; 97 102 Symbolic_t symbolic; 103 Qualified_t qualified; 98 104 DeclarationNode * tuple; 99 105 ExpressionNode * typeexpr; … … 103 109 void print( std::ostream &, int indent = 0 ) const; 104 110 TypeData * clone() const; 111 112 const std::string * leafName() const; 105 113 }; 106 114 -
src/Parser/parser.yy
rfc20514 r777ed2b 10 10 // Created On : Sat Sep 1 20:22:55 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jul 11 11: 20:51201813 // Update Count : 373 812 // Last Modified On : Wed Jul 11 11:55:24 2018 13 // Update Count : 3739 14 14 // 15 15 … … 1825 1825 { $$ = DeclarationNode::newFromTypedef( $1 ); } 1826 1826 | '.' TYPEDEFname 1827 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1827 { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), DeclarationNode::newFromTypedef( $2 ) ); } 1828 1828 | type_name '.' TYPEDEFname 1829 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1829 { $$ = DeclarationNode::newQualifiedType( $1, DeclarationNode::newFromTypedef( $3 ) ); } 1830 1830 | typegen_name 1831 1831 | '.' typegen_name 1832 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1832 { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), $2 ); } 1833 1833 | type_name '.' typegen_name 1834 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1834 { $$ = DeclarationNode::newQualifiedType( $1, $3 ); } 1835 1835 ; 1836 1836 … … 1863 1863 { forall = false; } // reset 1864 1864 '{' field_declaration_list_opt '}' type_parameters_opt 1865 { $$ = DeclarationNode::newAggregate( $1, n ew string( DeclarationNode::anonymous.newName() ), $7, $5, true )->addQualifiers( $2 ); }1865 { $$ = DeclarationNode::newAggregate( $1, nullptr, $7, $5, true )->addQualifiers( $2 ); } 1866 1866 | aggregate_key attribute_list_opt no_attr_identifier fred 1867 1867 { … … 1873 1873 | aggregate_key attribute_list_opt type_name fred 1874 1874 { 1875 typedefTable.makeTypedef( *$3->type->symbolic.name, forall | typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef 1875 // for type_name can be a qualified type name S.T, in which case only the last name in the chain needs a typedef (other names in the chain should already have one) 1876 typedefTable.makeTypedef( *$3->type->leafName(), forall | typedefTable.getEnclForall() ? TYPEGENname : TYPEDEFname ); // create typedef 1876 1877 forall = false; // reset 1877 1878 } … … 1936 1937 { distExt( $3 ); $$ = distAttr( $2, $3 ); } // mark all fields in list 1937 1938 | typedef_declaration ';' // CFA 1938 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }1939 1939 | cfa_field_declaring_list ';' // CFA, new style field declaration 1940 1940 | EXTENSION cfa_field_declaring_list ';' // GCC 1941 1941 { distExt( $2 ); $$ = $2; } // mark all fields in list 1942 1942 | cfa_typedef_declaration ';' // CFA 1943 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }1944 1943 | static_assert // C11 1945 1944 ; … … 1990 1989 enum_type: // enum 1991 1990 ENUM attribute_list_opt '{' enumerator_list comma_opt '}' 1992 { $$ = DeclarationNode::newEnum( n ew string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }1991 { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); } 1993 1992 | ENUM attribute_list_opt no_attr_identifier 1994 1993 { typedefTable.makeTypedef( *$3 ); }
Note:
See TracChangeset
for help on using the changeset viewer.