Changes in src/Parser/DeclarationNode.cc [033ff37:a1c9ddd]
- File:
-
- 1 edited
-
src/Parser/DeclarationNode.cc (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/DeclarationNode.cc
r033ff37 ra1c9ddd 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 l 25 22:17:10 201913 // Update Count : 1 11612 // Last Modified On : Thu Jun 7 12:08:55 2018 13 // Update Count : 1079 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", "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 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" }; 47 45 const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" }; 48 46 const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" }; 49 47 const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" }; 50 48 const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" }; 51 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", " __auto_type", "zero_t", "one_t", "NoBuiltinTypeNames" };49 const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" }; 52 50 53 51 UniqueName DeclarationNode::anonymous( "__anonymous" ); … … 56 54 57 55 DeclarationNode::DeclarationNode() : 58 linkage( ::linkage ) { 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 ) { 59 65 60 66 // variable.name = nullptr; … … 98 104 newnode->builtin = NoBuiltinType; 99 105 newnode->type = maybeClone( type ); 100 newnode->inLine = inLine;101 106 newnode->storageClasses = storageClasses; 102 107 newnode->funcSpecs = funcSpecs; … … 126 131 } // DeclarationNode::clone 127 132 128 void DeclarationNode::print( std::ostream & os, int indent ) const {133 void DeclarationNode::print( std::ostream &os, int indent ) const { 129 134 os << string( indent, ' ' ); 130 135 if ( name ) { … … 162 167 } 163 168 164 void DeclarationNode::printList( std::ostream & os, int indent ) const {169 void DeclarationNode::printList( std::ostream &os, int indent ) const { 165 170 ParseNode::printList( os, indent ); 166 171 if ( hasEllipsis ) { … … 249 254 } // DeclarationNode::newFromTypedef 250 255 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 269 256 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { 257 assert( name ); 270 258 DeclarationNode * newnode = new DeclarationNode; 271 259 newnode->type = new TypeData( TypeData::Aggregate ); 272 260 newnode->type->aggregate.kind = kind; 273 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) :name;261 newnode->type->aggregate.name = name; 274 262 newnode->type->aggregate.actuals = actuals; 275 263 newnode->type->aggregate.fields = fields; … … 277 265 newnode->type->aggregate.tagged = false; 278 266 newnode->type->aggregate.parent = nullptr; 279 newnode->type->aggregate.anon = name == nullptr;280 267 return newnode; 281 268 } // DeclarationNode::newAggregate 282 269 283 270 DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) { 271 assert( name ); 284 272 DeclarationNode * newnode = new DeclarationNode; 285 273 newnode->type = new TypeData( TypeData::Enum ); 286 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;274 newnode->type->enumeration.name = name; 287 275 newnode->type->enumeration.constants = constants; 288 276 newnode->type->enumeration.body = body; 289 newnode->type->enumeration.anon = name == nullptr;290 277 return newnode; 291 278 } // DeclarationNode::newEnum … … 404 391 } 405 392 406 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr , bool basetypeof) {407 DeclarationNode * newnode = new DeclarationNode; 408 newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof :TypeData::Typeof );393 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) { 394 DeclarationNode * newnode = new DeclarationNode; 395 newnode->type = new TypeData( TypeData::Typeof ); 409 396 newnode->type->typeexpr = expr; 410 397 return newnode; … … 418 405 return newnode; 419 406 } // 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 } 420 425 421 426 DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) { … … 498 503 } // DeclarationNode::copySpecifiers 499 504 500 static void addQualifiersToType( TypeData *& src, TypeData * dst ) { 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 501 514 if ( dst->base ) { 502 515 addQualifiersToType( src, dst->base ); … … 551 564 } // addQualifiers 552 565 553 static void addTypeToType( TypeData *& src, TypeData *&dst ) {566 static void addTypeToType( TypeData *&src, TypeData *&dst ) { 554 567 if ( src->forall && dst->kind == TypeData::Function ) { 555 568 if ( dst->forall ) { … … 942 955 } 943 956 944 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) {957 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) { 945 958 SemanticErrorException errors; 946 959 std::back_insert_iterator< std::list< Declaration * > > out( outputList ); … … 948 961 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 949 962 try { 950 bool extracted = false;951 bool anon = false;952 963 if ( DeclarationNode * extr = cur->extractAggregate() ) { 953 964 // handle the case where a structure declaration is contained within an object or type declaration 954 965 Declaration * decl = extr->build(); 955 966 if ( decl ) { 956 // hoist the structure declaration957 967 decl->location = cur->location; 958 968 * out++ = decl; 959 960 // need to remember the cases where a declaration contains an anonymous aggregate definition961 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 // 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; 975 decl->location = cur->location; 976 * out++ = decl; 977 } // if 978 } catch( SemanticErrorException &e ) { 979 errors.append( e ); 980 } // try 981 } // while 982 983 if ( ! errors.isEmpty() ) { 984 throw errors; 985 } // if 986 } // buildList 987 988 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { 989 SemanticErrorException errors; 990 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 991 992 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 993 try { 994 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; 997 1010 } // if 998 1011 } // if 999 } catch( SemanticErrorException & e ) {1012 } catch( SemanticErrorException &e ) { 1000 1013 errors.append( e ); 1001 1014 } // try … … 1007 1020 } // buildList 1008 1021 1009 // currently only builds assertions, function parameters, and return values 1010 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ) { 1011 SemanticErrorException errors; 1012 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); 1013 1014 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { 1015 try { 1016 Declaration * decl = cur->build(); 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; 1040 } // if 1041 } catch( SemanticErrorException & e ) { 1042 errors.append( e ); 1043 } // try 1044 } // for 1045 1046 if ( ! errors.isEmpty() ) { 1047 throw errors; 1048 } // if 1049 } // buildList 1050 1051 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) { 1022 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) { 1052 1023 SemanticErrorException errors; 1053 1024 std::back_insert_iterator< std::list< Type * > > out( outputList ); … … 1057 1028 try { 1058 1029 * out++ = cur->buildType(); 1059 } catch( SemanticErrorException & e ) {1030 } catch( SemanticErrorException &e ) { 1060 1031 errors.append( e ); 1061 1032 } // try … … 1093 1064 if ( type->kind != TypeData::Function && funcSpecs.any() ) { 1094 1065 SemanticError( this, "invalid function specifier for " ); 1095 } // if1096 // Forall qualifier can only appear on a function/aggregate definition/declaration.1097 //1098 // forall int f(); // allowed1099 // forall int g( int i ); // allowed1100 // forall int i; // disallowed1101 if ( type->kind != TypeData::Function && type->forall ) {1102 SemanticError( this, "invalid type qualifier for " );1103 1066 } // if 1104 1067 bool isDelete = initializer && initializer->get_isDelete();
Note:
See TracChangeset
for help on using the changeset viewer.