Changeset 3d7e53b for src/Parser
- Timestamp:
- Jul 10, 2018, 11:09:19 AM (6 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:
- 5cacf74
- Parents:
- aeec6b7
- Location:
- src/Parser
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
raeec6b7 r3d7e53b 273 273 274 274 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { 275 assert( name );276 275 DeclarationNode * newnode = new DeclarationNode; 277 276 newnode->type = new TypeData( TypeData::Aggregate ); 278 277 newnode->type->aggregate.kind = kind; 279 newnode->type->aggregate.name = name;278 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name; 280 279 newnode->type->aggregate.actuals = actuals; 281 280 newnode->type->aggregate.fields = fields; … … 283 282 newnode->type->aggregate.tagged = false; 284 283 newnode->type->aggregate.parent = nullptr; 284 newnode->type->aggregate.anon = name == nullptr; 285 285 return newnode; 286 286 } // DeclarationNode::newAggregate 287 287 288 288 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) { 289 assert( name );290 289 DeclarationNode * newnode = new DeclarationNode; 291 290 newnode->type = new TypeData( TypeData::Enum ); 292 newnode->type->enumeration.name = name ;291 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name; 293 292 newnode->type->enumeration.constants = constants; 294 293 newnode->type->enumeration.body = body; 294 newnode->type->enumeration.anon = name == nullptr; 295 295 return newnode; 296 296 } // DeclarationNode::newEnum … … 971 971 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 972 972 try { 973 bool extracted = false; 974 bool anon = false; 973 975 if ( DeclarationNode * extr = cur->extractAggregate() ) { 974 976 // handle the case where a structure declaration is contained within an object or type declaration 975 977 Declaration * decl = extr->build(); 976 978 if ( decl ) { 979 // hoist the structure declaration 977 980 decl->location = cur->location; 978 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 } 979 992 } // if 980 993 delete extr; … … 983 996 Declaration * decl = cur->build(); 984 997 if ( decl ) { 985 decl->location = cur->location; 986 * 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 } 987 1008 } // if 988 1009 } catch( SemanticErrorException &e ) { … … 996 1017 } // buildList 997 1018 1019 // currently only builds assertions, function parameters, and return values 998 1020 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 999 1021 SemanticErrorException errors; … … 1008 1030 * out++ = dwt; 1009 1031 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 1010 // xxx - this might be where anonymous struct members are added - should be conditional on struct name1032 // e.g., int foo(struct S) {} 1011 1033 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name ); 1012 1034 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); … … 1015 1037 delete agg; 1016 1038 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 1039 // e.g., int foo(union U) {} 1017 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 ); 1018 1047 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1019 1048 obj->location = cur->location; -
src/Parser/TypeData.cc
raeec6b7 r3d7e53b 63 63 enumeration.constants = nullptr; 64 64 enumeration.body = false; 65 enumeration.anon = false; 65 66 break; 66 67 case Aggregate: … … 74 75 aggregate.tagged = false; 75 76 aggregate.parent = nullptr; 77 aggregate.anon = false; 76 78 break; 77 79 case AggregateInst: … … 216 218 newtype->aggregate.fields = maybeClone( aggregate.fields ); 217 219 newtype->aggregate.body = aggregate.body; 220 newtype->aggregate.anon = aggregate.anon; 218 221 newtype->aggregate.tagged = aggregate.tagged; 219 222 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr; … … 228 231 newtype->enumeration.constants = maybeClone( enumeration.constants ); 229 232 newtype->enumeration.body = enumeration.body; 233 newtype->enumeration.anon = enumeration.anon; 230 234 break; 231 235 case Symbolic: -
src/Parser/TypeData.h
raeec6b7 r3d7e53b 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 -
src/Parser/parser.yy
raeec6b7 r3d7e53b 1857 1857 aggregate_type: // struct, union 1858 1858 aggregate_key attribute_list_opt '{' field_declaration_list_opt '}' type_parameters_opt 1859 { $$ = DeclarationNode::newAggregate( $1, n ew string( DeclarationNode::anonymous.newName() ), $6, $4, true )->addQualifiers( $2 ); }1859 { $$ = DeclarationNode::newAggregate( $1, nullptr, $6, $4, true )->addQualifiers( $2 ); } 1860 1860 | aggregate_key attribute_list_opt no_attr_identifier fred 1861 1861 { … … 1981 1981 enum_type: // enum 1982 1982 ENUM attribute_list_opt '{' enumerator_list comma_opt '}' 1983 { $$ = DeclarationNode::newEnum( n ew string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }1983 { $$ = DeclarationNode::newEnum( nullptr, $4, true )->addQualifiers( $2 ); } 1984 1984 | ENUM attribute_list_opt no_attr_identifier 1985 1985 { typedefTable.makeTypedef( *$3 ); }
Note: See TracChangeset
for help on using the changeset viewer.