Changes in src/Parser/DeclarationNode.cc [3d7e53b:284da8c]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r3d7e53b r284da8c 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 274 256 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { 257 assert( name ); 275 258 DeclarationNode * newnode = new DeclarationNode; 276 259 newnode->type = new TypeData( TypeData::Aggregate ); 277 260 newnode->type->aggregate.kind = kind; 278 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) :name;261 newnode->type->aggregate.name = name; 279 262 newnode->type->aggregate.actuals = actuals; 280 263 newnode->type->aggregate.fields = fields; … … 282 265 newnode->type->aggregate.tagged = false; 283 266 newnode->type->aggregate.parent = nullptr; 284 newnode->type->aggregate.anon = name == nullptr;285 267 return newnode; 286 268 } // DeclarationNode::newAggregate 287 269 288 270 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) { 271 assert( name ); 289 272 DeclarationNode * newnode = new DeclarationNode; 290 273 newnode->type = new TypeData( TypeData::Enum ); 291 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;274 newnode->type->enumeration.name = name; 292 275 newnode->type->enumeration.constants = constants; 293 276 newnode->type->enumeration.body = body; 294 newnode->type->enumeration.anon = name == nullptr;295 277 return newnode; 296 278 } // DeclarationNode::newEnum … … 971 953 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 972 954 try { 973 bool extracted = false;974 bool anon = false;975 955 if ( DeclarationNode * extr = cur->extractAggregate() ) { 976 956 // handle the case where a structure declaration is contained within an object or type declaration 977 957 Declaration * decl = extr->build(); 978 958 if ( decl ) { 979 // hoist the structure declaration980 959 decl->location = cur->location; 981 960 * out++ = decl; 982 983 // need to remember the cases where a declaration contains an anonymous aggregate definition984 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 }992 961 } // if 993 962 delete extr; … … 996 965 Declaration * decl = cur->build(); 997 966 if ( 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 } 967 decl->location = cur->location; 968 * out++ = decl; 1008 969 } // if 1009 970 } catch( SemanticErrorException &e ) { … … 1017 978 } // buildList 1018 979 1019 // currently only builds assertions, function parameters, and return values1020 980 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 1021 981 SemanticErrorException errors; … … 1025 985 try { 1026 986 Declaration * decl = cur->build(); 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; 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 1050 1003 } // if 1051 1004 } catch( SemanticErrorException &e ) {
Note:
See TracChangeset
for help on using the changeset viewer.