Changeset b067d9b for src/Parser/DeclarationNode.cc
- Timestamp:
- Oct 29, 2019, 4:01:24 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 773db65, 9421f3d8
- Parents:
- 7951100 (diff), 8364209 (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r7951100 rb067d9b 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Ju n 7 12:08:55 201813 // Update Count : 1 07912 // Last Modified On : Thu Jul 25 22:17:10 2019 13 // Update Count : 1116 14 14 // 15 15 … … 41 41 42 42 // These must harmonize with the corresponding DeclarationNode enumerations. 43 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" }; 44 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" }; 43 const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "int128", 44 "float", "double", "long double", "float80", "float128", 45 "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames" }; 46 const char * DeclarationNode::complexTypeNames[] = { "_Complex", "NoComplexTypeNames", "_Imaginary" }; // Imaginary unsupported => parse, but make invisible and print error message 45 47 const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" }; 46 48 const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" }; 47 49 const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" }; 48 50 const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" }; 49 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", " zero_t", "one_t", "NoBuiltinTypeNames" };51 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames" }; 50 52 51 53 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 54 56 55 57 DeclarationNode::DeclarationNode() : 56 builtin( NoBuiltinType ), 57 type( nullptr ), 58 bitfieldWidth( nullptr ), 59 hasEllipsis( false ), 60 linkage( ::linkage ), 61 asmName( nullptr ), 62 initializer( nullptr ), 63 extension( false ), 64 asmStmt( nullptr ) { 58 linkage( ::linkage ) { 65 59 66 60 // variable.name = nullptr; … … 104 98 newnode->builtin = NoBuiltinType; 105 99 newnode->type = maybeClone( type ); 100 newnode->inLine = inLine; 106 101 newnode->storageClasses = storageClasses; 107 102 newnode->funcSpecs = funcSpecs; … … 131 126 } // DeclarationNode::clone 132 127 133 void DeclarationNode::print( std::ostream & os, int indent ) const {128 void DeclarationNode::print( std::ostream & os, int indent ) const { 134 129 os << string( indent, ' ' ); 135 130 if ( name ) { … … 167 162 } 168 163 169 void DeclarationNode::printList( std::ostream & os, int indent ) const {164 void DeclarationNode::printList( std::ostream & os, int indent ) const { 170 165 ParseNode::printList( os, indent ); 171 166 if ( hasEllipsis ) { … … 254 249 } // DeclarationNode::newFromTypedef 255 250 251 DeclarationNode * DeclarationNode::newFromGlobalScope() { 252 DeclarationNode * newnode = new DeclarationNode; 253 newnode->type = new TypeData( TypeData::GlobalScope ); 254 return newnode; 255 } 256 257 DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) { 258 DeclarationNode * newnode = new DeclarationNode; 259 newnode->type = new TypeData( TypeData::Qualified ); 260 newnode->type->qualified.parent = parent->type; 261 newnode->type->qualified.child = child->type; 262 parent->type = nullptr; 263 child->type = nullptr; 264 delete parent; 265 delete child; 266 return newnode; 267 } 268 256 269 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { 257 assert( name );258 270 DeclarationNode * newnode = new DeclarationNode; 259 271 newnode->type = new TypeData( TypeData::Aggregate ); 260 272 newnode->type->aggregate.kind = kind; 261 newnode->type->aggregate.name = name;273 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name; 262 274 newnode->type->aggregate.actuals = actuals; 263 275 newnode->type->aggregate.fields = fields; … … 265 277 newnode->type->aggregate.tagged = false; 266 278 newnode->type->aggregate.parent = nullptr; 279 newnode->type->aggregate.anon = name == nullptr; 267 280 return newnode; 268 281 } // DeclarationNode::newAggregate 269 282 270 283 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) { 271 assert( name );272 284 DeclarationNode * newnode = new DeclarationNode; 273 285 newnode->type = new TypeData( TypeData::Enum ); 274 newnode->type->enumeration.name = name ;286 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name; 275 287 newnode->type->enumeration.constants = constants; 276 288 newnode->type->enumeration.body = body; 289 newnode->type->enumeration.anon = name == nullptr; 277 290 return newnode; 278 291 } // DeclarationNode::newEnum … … 391 404 } 392 405 393 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {394 DeclarationNode * newnode = new DeclarationNode; 395 newnode->type = new TypeData( TypeData::Typeof );406 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) { 407 DeclarationNode * newnode = new DeclarationNode; 408 newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof ); 396 409 newnode->type->typeexpr = expr; 397 410 return newnode; … … 405 418 return newnode; 406 419 } // DeclarationNode::newBuiltinType 407 408 DeclarationNode * DeclarationNode::newAttr( const string * name, ExpressionNode * expr ) {409 DeclarationNode * newnode = new DeclarationNode;410 newnode->type = nullptr;411 // newnode->attr.name = name;412 newnode->name = name;413 newnode->attr.expr = expr;414 return newnode;415 }416 417 DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {418 DeclarationNode * newnode = new DeclarationNode;419 newnode->type = nullptr;420 // newnode->attr.name = name;421 newnode->name = name;422 newnode->attr.type = type;423 return newnode;424 }425 420 426 421 DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) { … … 503 498 } // DeclarationNode::copySpecifiers 504 499 505 static void addQualifiersToType( TypeData *&src, TypeData * dst ) { 506 if ( src->forall && dst->kind == TypeData::Function ) { 507 if ( dst->forall ) { 508 dst->forall->appendList( src->forall ); 509 } else { 510 dst->forall = src->forall; 511 } // if 512 src->forall = nullptr; 513 } // if 500 static void addQualifiersToType( TypeData *& src, TypeData * dst ) { 514 501 if ( dst->base ) { 515 502 addQualifiersToType( src, dst->base ); … … 564 551 } // addQualifiers 565 552 566 static void addTypeToType( TypeData *& src, TypeData *&dst ) {553 static void addTypeToType( TypeData *& src, TypeData *& dst ) { 567 554 if ( src->forall && dst->kind == TypeData::Function ) { 568 555 if ( dst->forall ) { … … 955 942 } 956 943 957 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) {944 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) { 958 945 SemanticErrorException errors; 959 946 std::back_insert_iterator< std::list< Declaration * > > out( outputList ); … … 961 948 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 962 949 try { 950 bool extracted = false; 951 bool anon = false; 963 952 if ( DeclarationNode * extr = cur->extractAggregate() ) { 964 953 // handle the case where a structure declaration is contained within an object or type declaration 965 954 Declaration * decl = extr->build(); 966 955 if ( decl ) { 956 // hoist the structure declaration 967 957 decl->location = cur->location; 968 958 * out++ = decl; 959 960 // need to remember the cases where a declaration contains an anonymous aggregate definition 961 extracted = true; 962 assert( extr->type ); 963 if ( extr->type->kind == TypeData::Aggregate ) { 964 anon = extr->type->aggregate.anon; 965 } else if ( extr->type->kind == TypeData::Enum ) { 966 // xxx - is it useful to have an implicit anonymous enum member? 967 anon = extr->type->enumeration.anon; 968 } 969 969 } // if 970 970 delete extr; … … 973 973 Declaration * decl = cur->build(); 974 974 if ( decl ) { 975 decl->location = cur->location; 976 * out++ = decl; 975 // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.: 976 // struct S { 977 // struct T { int x; }; // no anonymous member 978 // struct { int y; }; // anonymous member 979 // struct T; // anonymous member 980 // }; 981 if ( ! (extracted && decl->name == "" && ! anon && ! cur->get_inLine()) ) { 982 if ( decl->name == "" ) { 983 if ( DeclarationWithType * dwt = dynamic_cast<DeclarationWithType *>( decl ) ) { 984 if ( ReferenceToType * aggr = dynamic_cast<ReferenceToType *>( dwt->get_type() ) ) { 985 if ( aggr->name.find("anonymous") == std::string::npos ) { 986 if ( ! cur->get_inLine() ) { 987 // temporary: warn about anonymous member declarations of named types, since 988 // this conflicts with the syntax for the forward declaration of an anonymous type 989 SemanticWarning( cur->location, Warning::AggrForwardDecl, aggr->name.c_str() ); 990 } // if 991 } // if 992 } // if 993 } // if 994 } // if 995 decl->location = cur->location; 996 *out++ = decl; 997 } // if 977 998 } // if 978 } catch( SemanticErrorException & e ) {999 } catch( SemanticErrorException & e ) { 979 1000 errors.append( e ); 980 1001 } // try 981 } // while1002 } // for 982 1003 983 1004 if ( ! errors.isEmpty() ) { … … 986 1007 } // buildList 987 1008 988 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 1009 // currently only builds assertions, function parameters, and return values 1010 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ) { 989 1011 SemanticErrorException errors; 990 1012 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); … … 993 1015 try { 994 1016 Declaration * decl = cur->build(); 995 if ( decl ) { 996 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 997 dwt->location = cur->location; 998 * out++ = dwt; 999 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 1000 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); 1001 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1002 obj->location = cur->location; 1003 * out++ = obj; 1004 delete agg; 1005 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 1006 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); 1007 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1008 obj->location = cur->location; 1009 * out++ = obj; 1010 } // if 1017 assert( decl ); 1018 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 1019 dwt->location = cur->location; 1020 * out++ = dwt; 1021 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 1022 // e.g., int foo(struct S) {} 1023 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name ); 1024 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1025 obj->location = cur->location; 1026 * out++ = obj; 1027 delete agg; 1028 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 1029 // e.g., int foo(union U) {} 1030 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name ); 1031 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1032 obj->location = cur->location; 1033 * out++ = obj; 1034 } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) { 1035 // e.g., int foo(enum E) {} 1036 EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name ); 1037 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1038 obj->location = cur->location; 1039 * out++ = obj; 1011 1040 } // if 1012 } catch( SemanticErrorException & e ) {1041 } catch( SemanticErrorException & e ) { 1013 1042 errors.append( e ); 1014 1043 } // try … … 1020 1049 } // buildList 1021 1050 1022 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) {1051 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) { 1023 1052 SemanticErrorException errors; 1024 1053 std::back_insert_iterator< std::list< Type * > > out( outputList ); … … 1028 1057 try { 1029 1058 * out++ = cur->buildType(); 1030 } catch( SemanticErrorException & e ) {1059 } catch( SemanticErrorException & e ) { 1031 1060 errors.append( e ); 1032 1061 } // try … … 1064 1093 if ( type->kind != TypeData::Function && funcSpecs.any() ) { 1065 1094 SemanticError( this, "invalid function specifier for " ); 1095 } // if 1096 // Forall qualifier can only appear on a function/aggregate definition/declaration. 1097 // 1098 // forall int f(); // allowed 1099 // forall int g( int i ); // allowed 1100 // forall int i; // disallowed 1101 if ( type->kind != TypeData::Function && type->forall ) { 1102 SemanticError( this, "invalid type qualifier for " ); 1066 1103 } // if 1067 1104 bool isDelete = initializer && initializer->get_isDelete();
Note:
See TracChangeset
for help on using the changeset viewer.