[b87a5ed] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[974906e2] | 7 | // DeclarationNode.cc -- |
---|
[b87a5ed] | 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 12:34:05 2015 |
---|
[8e9cbb2] | 11 | // Last Modified By : Peter A. Buhr |
---|
[e994912] | 12 | // Last Modified On : Thu Feb 9 14:48:40 2017 |
---|
| 13 | // Update Count : 741 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <string> |
---|
| 17 | #include <list> |
---|
| 18 | #include <iterator> |
---|
| 19 | #include <algorithm> |
---|
| 20 | #include <cassert> |
---|
| 21 | |
---|
| 22 | #include "TypeData.h" |
---|
[bdd516a] | 23 | |
---|
[44a81853] | 24 | #include "SynTree/Attribute.h" |
---|
[68cd1ce] | 25 | #include "SynTree/Declaration.h" |
---|
| 26 | #include "SynTree/Expression.h" |
---|
[51b7345] | 27 | |
---|
[de62360d] | 28 | #include "TypedefTable.h" |
---|
| 29 | extern TypedefTable typedefTable; |
---|
| 30 | |
---|
[51b7345] | 31 | using namespace std; |
---|
| 32 | |
---|
[bdd516a] | 33 | // These must remain in the same order as the corresponding DeclarationNode enumerations. |
---|
[ba7aa2d] | 34 | const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" }; |
---|
| 35 | const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" }; |
---|
| 36 | const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" }; |
---|
| 37 | const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" }; |
---|
| 38 | const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" }; |
---|
| 39 | const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" }; |
---|
| 40 | const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" }; |
---|
| 41 | const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" }; |
---|
| 42 | const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" }; |
---|
[51b7345] | 43 | |
---|
| 44 | UniqueName DeclarationNode::anonymous( "__anonymous" ); |
---|
| 45 | |
---|
[8b7ee09] | 46 | extern LinkageSpec::Spec linkage; // defined in parser.yy |
---|
[51b7345] | 47 | |
---|
[7d05e7e] | 48 | DeclarationNode::DeclarationNode() : |
---|
[2298f728] | 49 | type( nullptr ), |
---|
[7d05e7e] | 50 | storageClass( NoStorageClass ), |
---|
[58dd019] | 51 | bitfieldWidth( nullptr ), |
---|
[7d05e7e] | 52 | isInline( false ), |
---|
| 53 | isNoreturn( false ), |
---|
| 54 | hasEllipsis( false ), |
---|
| 55 | linkage( ::linkage ), |
---|
[58dd019] | 56 | asmName( nullptr ), |
---|
| 57 | initializer( nullptr ), |
---|
[e994912] | 58 | extension( false ), |
---|
| 59 | asmStmt( nullptr ) { |
---|
[2298f728] | 60 | |
---|
[faddbd8] | 61 | // variable.name = nullptr; |
---|
| 62 | variable.tyClass = NoTypeClass; |
---|
[28307be] | 63 | variable.assertions = nullptr; |
---|
[7d05e7e] | 64 | |
---|
[faddbd8] | 65 | // attr.name = nullptr; |
---|
[7d05e7e] | 66 | attr.expr = nullptr; |
---|
| 67 | attr.type = nullptr; |
---|
[28307be] | 68 | } |
---|
| 69 | |
---|
| 70 | DeclarationNode::~DeclarationNode() { |
---|
[faddbd8] | 71 | // delete attr.name; |
---|
[28307be] | 72 | delete attr.expr; |
---|
| 73 | delete attr.type; |
---|
[2298f728] | 74 | |
---|
[faddbd8] | 75 | // delete variable.name; |
---|
[2298f728] | 76 | delete variable.assertions; |
---|
| 77 | |
---|
[28307be] | 78 | delete type; |
---|
| 79 | delete bitfieldWidth; |
---|
[e994912] | 80 | |
---|
| 81 | delete asmStmt; |
---|
[58dd019] | 82 | // asmName, no delete, passed to next stage |
---|
[28307be] | 83 | delete initializer; |
---|
| 84 | } |
---|
| 85 | |
---|
[ba7aa2d] | 86 | DeclarationNode * DeclarationNode::clone() const { |
---|
| 87 | DeclarationNode * newnode = new DeclarationNode; |
---|
[c0aa336] | 88 | newnode->set_next( maybeClone( get_next() ) ); |
---|
[2298f728] | 89 | newnode->name = name ? new string( *name ) : nullptr; |
---|
[c0aa336] | 90 | |
---|
| 91 | newnode->type = maybeClone( type ); |
---|
[13e3b50] | 92 | newnode->storageClass = storageClass; |
---|
[c0aa336] | 93 | newnode->bitfieldWidth = maybeClone( bitfieldWidth ); |
---|
[13e3b50] | 94 | newnode->isInline = isInline; |
---|
| 95 | newnode->isNoreturn = isNoreturn; |
---|
[c0aa336] | 96 | newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) ); |
---|
[b87a5ed] | 97 | newnode->hasEllipsis = hasEllipsis; |
---|
| 98 | newnode->linkage = linkage; |
---|
[58dd019] | 99 | newnode->asmName = maybeClone( asmName ); |
---|
[c0aa336] | 100 | cloneAll( attributes, newnode->attributes ); |
---|
| 101 | newnode->initializer = maybeClone( initializer ); |
---|
| 102 | newnode->extension = extension; |
---|
[e994912] | 103 | newnode->asmStmt = maybeClone( asmStmt ); |
---|
[c0aa336] | 104 | newnode->error = error; |
---|
[3848e0e] | 105 | |
---|
[faddbd8] | 106 | // newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr; |
---|
[28307be] | 107 | newnode->variable.tyClass = variable.tyClass; |
---|
[fb114fa1] | 108 | newnode->variable.assertions = maybeClone( variable.assertions ); |
---|
[3848e0e] | 109 | |
---|
[faddbd8] | 110 | // newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr; |
---|
[28307be] | 111 | newnode->attr.expr = maybeClone( attr.expr ); |
---|
| 112 | newnode->attr.type = maybeClone( attr.type ); |
---|
| 113 | return newnode; |
---|
| 114 | } // DeclarationNode::clone |
---|
[3848e0e] | 115 | |
---|
| 116 | bool DeclarationNode::get_hasEllipsis() const { |
---|
[b87a5ed] | 117 | return hasEllipsis; |
---|
[3848e0e] | 118 | } |
---|
| 119 | |
---|
| 120 | void DeclarationNode::print( std::ostream &os, int indent ) const { |
---|
[59db689] | 121 | os << string( indent, ' ' ); |
---|
[2298f728] | 122 | if ( name ) { |
---|
| 123 | os << *name << ": "; |
---|
[b87a5ed] | 124 | } else { |
---|
[2298f728] | 125 | os << "unnamed: "; |
---|
[68cd1ce] | 126 | } // if |
---|
[51b7345] | 127 | |
---|
[b87a5ed] | 128 | if ( linkage != LinkageSpec::Cforall ) { |
---|
[faddbd8] | 129 | os << LinkageSpec::linkageName( linkage ) << " "; |
---|
[68cd1ce] | 130 | } // if |
---|
[3848e0e] | 131 | |
---|
[c1c1112] | 132 | if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' '; |
---|
| 133 | if ( isInline ) os << DeclarationNode::storageName[Inline] << ' '; |
---|
| 134 | if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' '; |
---|
[b87a5ed] | 135 | if ( type ) { |
---|
| 136 | type->print( os, indent ); |
---|
| 137 | } else { |
---|
| 138 | os << "untyped entity "; |
---|
[68cd1ce] | 139 | } // if |
---|
[3848e0e] | 140 | |
---|
[b87a5ed] | 141 | if ( bitfieldWidth ) { |
---|
[59db689] | 142 | os << endl << string( indent + 2, ' ' ) << "with bitfield width "; |
---|
[b87a5ed] | 143 | bitfieldWidth->printOneLine( os ); |
---|
[68cd1ce] | 144 | } // if |
---|
[3848e0e] | 145 | |
---|
[2298f728] | 146 | if ( initializer ) { |
---|
[59db689] | 147 | os << endl << string( indent + 2, ' ' ) << "with initializer "; |
---|
[b87a5ed] | 148 | initializer->printOneLine( os ); |
---|
[974906e2] | 149 | os << " maybe constructed? " << initializer->get_maybeConstructed(); |
---|
| 150 | |
---|
[68cd1ce] | 151 | } // if |
---|
[3848e0e] | 152 | |
---|
[b87a5ed] | 153 | os << endl; |
---|
[51b7345] | 154 | } |
---|
| 155 | |
---|
[3848e0e] | 156 | void DeclarationNode::printList( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 157 | ParseNode::printList( os, indent ); |
---|
| 158 | if ( hasEllipsis ) { |
---|
| 159 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; |
---|
[68cd1ce] | 160 | } // if |
---|
[51b7345] | 161 | } |
---|
| 162 | |
---|
[fb114fa1] | 163 | DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) { |
---|
[ba7aa2d] | 164 | DeclarationNode * newnode = new DeclarationNode; |
---|
[2298f728] | 165 | newnode->name = name; |
---|
[b87a5ed] | 166 | newnode->type = new TypeData( TypeData::Function ); |
---|
[8f6f47d7] | 167 | newnode->type->function.params = param; |
---|
| 168 | newnode->type->function.newStyle = newStyle; |
---|
| 169 | newnode->type->function.body = body; |
---|
[c0aa336] | 170 | |
---|
[2298f728] | 171 | // ignore unnamed routine declarations: void p( int (*)(int) ); |
---|
| 172 | if ( newnode->name ) { |
---|
| 173 | typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); |
---|
| 174 | } // if |
---|
[3848e0e] | 175 | |
---|
[b87a5ed] | 176 | if ( body ) { |
---|
[8f6f47d7] | 177 | newnode->type->function.hasBody = true; |
---|
[68cd1ce] | 178 | } // if |
---|
[51b7345] | 179 | |
---|
[b87a5ed] | 180 | if ( ret ) { |
---|
| 181 | newnode->type->base = ret->type; |
---|
[2298f728] | 182 | ret->type = nullptr; |
---|
[b87a5ed] | 183 | delete ret; |
---|
[68cd1ce] | 184 | } // if |
---|
[51b7345] | 185 | |
---|
[b87a5ed] | 186 | return newnode; |
---|
[984dce6] | 187 | } // DeclarationNode::newFunction |
---|
[3848e0e] | 188 | |
---|
[c1c1112] | 189 | DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) { |
---|
[ba7aa2d] | 190 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 191 | newnode->type = new TypeData(); |
---|
[c1c1112] | 192 | newnode->type->qualifiers[ q ] = 1; |
---|
[b87a5ed] | 193 | return newnode; |
---|
[984dce6] | 194 | } // DeclarationNode::newQualifier |
---|
[3848e0e] | 195 | |
---|
[ba7aa2d] | 196 | DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { |
---|
| 197 | DeclarationNode * newnode = new DeclarationNode; |
---|
[c1c1112] | 198 | newnode->type = new TypeData( TypeData::Unknown ); |
---|
| 199 | newnode->type->forall = forall; |
---|
| 200 | return newnode; |
---|
| 201 | } // DeclarationNode::newForall |
---|
| 202 | |
---|
| 203 | DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) { |
---|
[ba7aa2d] | 204 | DeclarationNode * newnode = new DeclarationNode; |
---|
[c1c1112] | 205 | newnode->storageClass = sc; |
---|
[b87a5ed] | 206 | return newnode; |
---|
[984dce6] | 207 | } // DeclarationNode::newStorageClass |
---|
[3848e0e] | 208 | |
---|
[c1c1112] | 209 | DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { |
---|
[ba7aa2d] | 210 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 211 | newnode->type = new TypeData( TypeData::Basic ); |
---|
[5b639ee] | 212 | newnode->type->basictype = bt; |
---|
[b87a5ed] | 213 | return newnode; |
---|
[984dce6] | 214 | } // DeclarationNode::newBasicType |
---|
[3848e0e] | 215 | |
---|
[5b639ee] | 216 | DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { |
---|
[ba7aa2d] | 217 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 218 | newnode->type = new TypeData( TypeData::Basic ); |
---|
[5b639ee] | 219 | newnode->type->complextype = ct; |
---|
[b87a5ed] | 220 | return newnode; |
---|
[5b639ee] | 221 | } // DeclarationNode::newComplexType |
---|
| 222 | |
---|
| 223 | DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { |
---|
[ba7aa2d] | 224 | DeclarationNode * newnode = new DeclarationNode; |
---|
[5b639ee] | 225 | newnode->type = new TypeData( TypeData::Basic ); |
---|
| 226 | newnode->type->signedness = sn; |
---|
| 227 | return newnode; |
---|
| 228 | } // DeclarationNode::newSignedNess |
---|
| 229 | |
---|
| 230 | DeclarationNode * DeclarationNode::newLength( Length lnth ) { |
---|
[ba7aa2d] | 231 | DeclarationNode * newnode = new DeclarationNode; |
---|
[5b639ee] | 232 | newnode->type = new TypeData( TypeData::Basic ); |
---|
| 233 | newnode->type->length = lnth; |
---|
| 234 | return newnode; |
---|
| 235 | } // DeclarationNode::newLength |
---|
[3848e0e] | 236 | |
---|
[fb114fa1] | 237 | DeclarationNode * DeclarationNode::newFromTypedef( string * name ) { |
---|
[ba7aa2d] | 238 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 239 | newnode->type = new TypeData( TypeData::SymbolicInst ); |
---|
[fb114fa1] | 240 | newnode->type->symbolic.name = name; |
---|
[8f6f47d7] | 241 | newnode->type->symbolic.isTypedef = true; |
---|
[2298f728] | 242 | newnode->type->symbolic.params = nullptr; |
---|
[b87a5ed] | 243 | return newnode; |
---|
[984dce6] | 244 | } // DeclarationNode::newFromTypedef |
---|
[3848e0e] | 245 | |
---|
[fb114fa1] | 246 | DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { |
---|
[ba7aa2d] | 247 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 248 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
[8f6f47d7] | 249 | newnode->type->aggregate.kind = kind; |
---|
[2298f728] | 250 | if ( name ) { |
---|
[fb114fa1] | 251 | newnode->type->aggregate.name = name; |
---|
[2298f728] | 252 | } else { // anonymous aggregate ? |
---|
| 253 | newnode->type->aggregate.name = new string( anonymous.newName() ); |
---|
[8f6f47d7] | 254 | } // if |
---|
| 255 | newnode->type->aggregate.actuals = actuals; |
---|
| 256 | newnode->type->aggregate.fields = fields; |
---|
| 257 | newnode->type->aggregate.body = body; |
---|
[b87a5ed] | 258 | return newnode; |
---|
[984dce6] | 259 | } // DeclarationNode::newAggregate |
---|
[3848e0e] | 260 | |
---|
[fb114fa1] | 261 | DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) { |
---|
[ba7aa2d] | 262 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 263 | newnode->type = new TypeData( TypeData::Enum ); |
---|
[2298f728] | 264 | if ( name ) { |
---|
[fb114fa1] | 265 | newnode->type->enumeration.name = name; |
---|
[2298f728] | 266 | } else { // anonymous aggregate ? |
---|
| 267 | newnode->type->enumeration.name = new string( anonymous.newName() ); |
---|
[68cd1ce] | 268 | } // if |
---|
[8f6f47d7] | 269 | newnode->type->enumeration.constants = constants; |
---|
[b87a5ed] | 270 | return newnode; |
---|
[984dce6] | 271 | } // DeclarationNode::newEnum |
---|
[3848e0e] | 272 | |
---|
[fb114fa1] | 273 | DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) { |
---|
[ba7aa2d] | 274 | DeclarationNode * newnode = new DeclarationNode; |
---|
[2298f728] | 275 | newnode->name = name; |
---|
[4f147cc] | 276 | newnode->enumeratorValue.reset( constant ); |
---|
[2298f728] | 277 | typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID ); |
---|
[b87a5ed] | 278 | return newnode; |
---|
[984dce6] | 279 | } // DeclarationNode::newEnumConstant |
---|
[3848e0e] | 280 | |
---|
[fb114fa1] | 281 | DeclarationNode * DeclarationNode::newName( string * name ) { |
---|
[ba7aa2d] | 282 | DeclarationNode * newnode = new DeclarationNode; |
---|
[2298f728] | 283 | newnode->name = name; |
---|
[b87a5ed] | 284 | return newnode; |
---|
[984dce6] | 285 | } // DeclarationNode::newName |
---|
[3848e0e] | 286 | |
---|
[fb114fa1] | 287 | DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) { |
---|
[ba7aa2d] | 288 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 289 | newnode->type = new TypeData( TypeData::SymbolicInst ); |
---|
[fb114fa1] | 290 | newnode->type->symbolic.name = name; |
---|
[8f6f47d7] | 291 | newnode->type->symbolic.isTypedef = false; |
---|
| 292 | newnode->type->symbolic.actuals = params; |
---|
[b87a5ed] | 293 | return newnode; |
---|
[984dce6] | 294 | } // DeclarationNode::newFromTypeGen |
---|
[3848e0e] | 295 | |
---|
[fb114fa1] | 296 | DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) { |
---|
[ba7aa2d] | 297 | DeclarationNode * newnode = new DeclarationNode; |
---|
[2298f728] | 298 | newnode->type = nullptr; |
---|
[148f7290] | 299 | assert( ! newnode->name ); |
---|
[faddbd8] | 300 | // newnode->variable.name = name; |
---|
| 301 | newnode->name = name; |
---|
[28307be] | 302 | newnode->variable.tyClass = tc; |
---|
[faddbd8] | 303 | newnode->variable.assertions = nullptr; |
---|
[b87a5ed] | 304 | return newnode; |
---|
[984dce6] | 305 | } // DeclarationNode::newTypeParam |
---|
[3848e0e] | 306 | |
---|
[fb114fa1] | 307 | DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) { |
---|
[ba7aa2d] | 308 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 309 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
[2298f728] | 310 | newnode->type->aggregate.name = name; |
---|
[8f6f47d7] | 311 | newnode->type->aggregate.kind = Trait; |
---|
| 312 | newnode->type->aggregate.params = params; |
---|
| 313 | newnode->type->aggregate.fields = asserts; |
---|
[b87a5ed] | 314 | return newnode; |
---|
[984dce6] | 315 | } // DeclarationNode::newTrait |
---|
[3848e0e] | 316 | |
---|
[fb114fa1] | 317 | DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) { |
---|
[ba7aa2d] | 318 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 319 | newnode->type = new TypeData( TypeData::AggregateInst ); |
---|
[8f6f47d7] | 320 | newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); |
---|
| 321 | newnode->type->aggInst.aggregate->aggregate.kind = Trait; |
---|
[2298f728] | 322 | newnode->type->aggInst.aggregate->aggregate.name = name; |
---|
[8f6f47d7] | 323 | newnode->type->aggInst.params = params; |
---|
[b87a5ed] | 324 | return newnode; |
---|
[984dce6] | 325 | } // DeclarationNode::newTraitUse |
---|
[3848e0e] | 326 | |
---|
[fb114fa1] | 327 | DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) { |
---|
[ba7aa2d] | 328 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 329 | newnode->type = new TypeData( TypeData::Symbolic ); |
---|
[8f6f47d7] | 330 | newnode->type->symbolic.isTypedef = false; |
---|
| 331 | newnode->type->symbolic.params = typeParams; |
---|
[fb114fa1] | 332 | newnode->type->symbolic.name = name; |
---|
[b87a5ed] | 333 | return newnode; |
---|
[984dce6] | 334 | } // DeclarationNode::newTypeDecl |
---|
[3848e0e] | 335 | |
---|
[ba7aa2d] | 336 | DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) { |
---|
| 337 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 338 | newnode->type = new TypeData( TypeData::Pointer ); |
---|
| 339 | return newnode->addQualifiers( qualifiers ); |
---|
[984dce6] | 340 | } // DeclarationNode::newPointer |
---|
[3848e0e] | 341 | |
---|
[ba7aa2d] | 342 | DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) { |
---|
| 343 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 344 | newnode->type = new TypeData( TypeData::Array ); |
---|
[8f6f47d7] | 345 | newnode->type->array.dimension = size; |
---|
| 346 | newnode->type->array.isStatic = isStatic; |
---|
[2298f728] | 347 | if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) { |
---|
[8f6f47d7] | 348 | newnode->type->array.isVarLen = false; |
---|
[71bd8c6] | 349 | } else { |
---|
[8f6f47d7] | 350 | newnode->type->array.isVarLen = true; |
---|
[71bd8c6] | 351 | } // if |
---|
[b87a5ed] | 352 | return newnode->addQualifiers( qualifiers ); |
---|
[984dce6] | 353 | } // DeclarationNode::newArray |
---|
[3848e0e] | 354 | |
---|
[ba7aa2d] | 355 | DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) { |
---|
| 356 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 357 | newnode->type = new TypeData( TypeData::Array ); |
---|
[2298f728] | 358 | newnode->type->array.dimension = nullptr; |
---|
[8f6f47d7] | 359 | newnode->type->array.isStatic = false; |
---|
| 360 | newnode->type->array.isVarLen = true; |
---|
[b87a5ed] | 361 | return newnode->addQualifiers( qualifiers ); |
---|
[3848e0e] | 362 | } |
---|
| 363 | |
---|
[ba7aa2d] | 364 | DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) { |
---|
| 365 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 366 | newnode->bitfieldWidth = size; |
---|
| 367 | return newnode; |
---|
[3848e0e] | 368 | } |
---|
| 369 | |
---|
[ba7aa2d] | 370 | DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) { |
---|
| 371 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 372 | newnode->type = new TypeData( TypeData::Tuple ); |
---|
[8f6f47d7] | 373 | newnode->type->tuple = members; |
---|
[b87a5ed] | 374 | return newnode; |
---|
[3848e0e] | 375 | } |
---|
| 376 | |
---|
[ba7aa2d] | 377 | DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) { |
---|
| 378 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 379 | newnode->type = new TypeData( TypeData::Typeof ); |
---|
[8f6f47d7] | 380 | newnode->type->typeexpr = expr; |
---|
[b87a5ed] | 381 | return newnode; |
---|
[3848e0e] | 382 | } |
---|
| 383 | |
---|
[8f6f47d7] | 384 | DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { |
---|
[ba7aa2d] | 385 | DeclarationNode * newnode = new DeclarationNode; |
---|
[8f6f47d7] | 386 | newnode->type = new TypeData( TypeData::Builtin ); |
---|
| 387 | newnode->builtin = bt; |
---|
[148f7290] | 388 | newnode->type->builtintype = newnode->builtin; |
---|
[8f6f47d7] | 389 | return newnode; |
---|
| 390 | } // DeclarationNode::newBuiltinType |
---|
| 391 | |
---|
[fb114fa1] | 392 | DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) { |
---|
[ba7aa2d] | 393 | DeclarationNode * newnode = new DeclarationNode; |
---|
[2298f728] | 394 | newnode->type = nullptr; |
---|
[faddbd8] | 395 | // newnode->attr.name = name; |
---|
| 396 | newnode->name = name; |
---|
[28307be] | 397 | newnode->attr.expr = expr; |
---|
[b87a5ed] | 398 | return newnode; |
---|
[3848e0e] | 399 | } |
---|
| 400 | |
---|
[fb114fa1] | 401 | DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) { |
---|
[ba7aa2d] | 402 | DeclarationNode * newnode = new DeclarationNode; |
---|
[2298f728] | 403 | newnode->type = nullptr; |
---|
[faddbd8] | 404 | // newnode->attr.name = name; |
---|
| 405 | newnode->name = name; |
---|
[28307be] | 406 | newnode->attr.type = type; |
---|
[b87a5ed] | 407 | return newnode; |
---|
[3848e0e] | 408 | } |
---|
| 409 | |
---|
[44a81853] | 410 | DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) { |
---|
| 411 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 412 | newnode->type = nullptr; |
---|
| 413 | std::list< Expression * > exprs; |
---|
| 414 | buildList( expr, exprs ); |
---|
| 415 | newnode->attributes.push_back( new Attribute( *name, exprs ) ); |
---|
| 416 | delete name; |
---|
| 417 | return newnode; |
---|
| 418 | } |
---|
| 419 | |
---|
[e994912] | 420 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) { |
---|
| 421 | DeclarationNode * newnode = new DeclarationNode; |
---|
| 422 | newnode->asmStmt = stmt; |
---|
| 423 | return newnode; |
---|
| 424 | } |
---|
| 425 | |
---|
[5b639ee] | 426 | void appendError( string & dst, const string & src ) { |
---|
| 427 | if ( src.empty() ) return; |
---|
| 428 | if ( dst.empty() ) { dst = src; return; } |
---|
| 429 | dst += ", " + src; |
---|
| 430 | } // appendError |
---|
| 431 | |
---|
[ba7aa2d] | 432 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { |
---|
[b6424d9] | 433 | TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization |
---|
[c1c1112] | 434 | |
---|
[b6424d9] | 435 | if ( (qsrc & qdst).any() ) { // common qualifier ? |
---|
[5b639ee] | 436 | for ( int i = 0; i < NoQualifier; i += 1 ) { // find common qualifiers |
---|
| 437 | if ( qsrc[i] && qdst[i] ) { |
---|
| 438 | appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] ); |
---|
[c1c1112] | 439 | } // if |
---|
| 440 | } // for |
---|
| 441 | } // if |
---|
| 442 | } // DeclarationNode::checkQualifiers |
---|
| 443 | |
---|
[ba7aa2d] | 444 | void DeclarationNode::checkStorageClasses( DeclarationNode * q ) { |
---|
[b6424d9] | 445 | if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) { |
---|
| 446 | if ( storageClass == q->storageClass ) { // duplicate qualifier |
---|
[5b639ee] | 447 | appendError( error, string( "duplicate " ) + storageName[ storageClass ] ); |
---|
[b6424d9] | 448 | } else { // only one storage class |
---|
[5b639ee] | 449 | appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] ); |
---|
| 450 | q->storageClass = storageClass; // FIX ERROR, prevent assertions from triggering |
---|
[b6424d9] | 451 | } // if |
---|
| 452 | } // if |
---|
[5b639ee] | 453 | appendError( error, q->error ); |
---|
[c0aa336] | 454 | } // DeclarationNode::checkStorageClasses |
---|
[b6424d9] | 455 | |
---|
[ba7aa2d] | 456 | DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) { |
---|
[5b639ee] | 457 | isInline = isInline || q->isInline; |
---|
| 458 | isNoreturn = isNoreturn || q->isNoreturn; |
---|
[b6424d9] | 459 | // do not overwrite an existing value with NoStorageClass |
---|
| 460 | if ( q->storageClass != NoStorageClass ) { |
---|
| 461 | assert( storageClass == NoStorageClass || storageClass == q->storageClass ); |
---|
| 462 | storageClass = q->storageClass; |
---|
| 463 | } // if |
---|
[c0aa336] | 464 | |
---|
| 465 | for ( Attribute *attr: reverseIterate( q->attributes ) ) { |
---|
| 466 | attributes.push_front( attr->clone() ); |
---|
| 467 | } // for |
---|
[b6424d9] | 468 | return this; |
---|
| 469 | } // DeclarationNode::copyStorageClasses |
---|
| 470 | |
---|
[ba7aa2d] | 471 | static void addQualifiersToType( TypeData *&src, TypeData * dst ) { |
---|
[101e0bd] | 472 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
| 473 | if ( dst->forall ) { |
---|
| 474 | dst->forall->appendList( src->forall ); |
---|
| 475 | } else { |
---|
| 476 | dst->forall = src->forall; |
---|
| 477 | } // if |
---|
[2298f728] | 478 | src->forall = nullptr; |
---|
[101e0bd] | 479 | } // if |
---|
| 480 | if ( dst->base ) { |
---|
| 481 | addQualifiersToType( src, dst->base ); |
---|
| 482 | } else if ( dst->kind == TypeData::Function ) { |
---|
| 483 | dst->base = src; |
---|
[2298f728] | 484 | src = nullptr; |
---|
[101e0bd] | 485 | } else { |
---|
| 486 | dst->qualifiers |= src->qualifiers; |
---|
| 487 | } // if |
---|
| 488 | } // addQualifiersToType |
---|
| 489 | |
---|
[ba7aa2d] | 490 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { |
---|
[2298f728] | 491 | if ( ! q ) { delete q; return this; } |
---|
[101e0bd] | 492 | |
---|
| 493 | checkStorageClasses( q ); |
---|
| 494 | copyStorageClasses( q ); |
---|
| 495 | |
---|
[1b77274] | 496 | if ( ! q->type ) { |
---|
| 497 | delete q; |
---|
| 498 | return this; |
---|
| 499 | } // if |
---|
[101e0bd] | 500 | |
---|
| 501 | if ( ! type ) { |
---|
[1b77274] | 502 | type = q->type; // reuse this structure |
---|
| 503 | q->type = nullptr; |
---|
| 504 | delete q; |
---|
[101e0bd] | 505 | return this; |
---|
| 506 | } // if |
---|
| 507 | |
---|
| 508 | checkQualifiers( q->type, type ); |
---|
| 509 | addQualifiersToType( q->type, type ); |
---|
| 510 | |
---|
| 511 | if ( q->type->forall ) { |
---|
| 512 | if ( type->forall ) { |
---|
| 513 | type->forall->appendList( q->type->forall ); |
---|
| 514 | } else { |
---|
| 515 | if ( type->kind == TypeData::Aggregate ) { |
---|
| 516 | type->aggregate.params = q->type->forall; |
---|
| 517 | // change implicit typedef from TYPEDEFname to TYPEGENname |
---|
[2298f728] | 518 | typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG ); |
---|
[c1c1112] | 519 | } else { |
---|
[101e0bd] | 520 | type->forall = q->type->forall; |
---|
[68cd1ce] | 521 | } // if |
---|
| 522 | } // if |
---|
[2298f728] | 523 | q->type->forall = nullptr; |
---|
[68cd1ce] | 524 | } // if |
---|
[b87a5ed] | 525 | delete q; |
---|
| 526 | return this; |
---|
[101e0bd] | 527 | } // addQualifiers |
---|
[3848e0e] | 528 | |
---|
| 529 | static void addTypeToType( TypeData *&src, TypeData *&dst ) { |
---|
[101e0bd] | 530 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
| 531 | if ( dst->forall ) { |
---|
| 532 | dst->forall->appendList( src->forall ); |
---|
[b87a5ed] | 533 | } else { |
---|
[101e0bd] | 534 | dst->forall = src->forall; |
---|
| 535 | } // if |
---|
[2298f728] | 536 | src->forall = nullptr; |
---|
[101e0bd] | 537 | } // if |
---|
| 538 | if ( dst->base ) { |
---|
| 539 | addTypeToType( src, dst->base ); |
---|
| 540 | } else { |
---|
| 541 | switch ( dst->kind ) { |
---|
| 542 | case TypeData::Unknown: |
---|
| 543 | src->qualifiers |= dst->qualifiers; |
---|
| 544 | dst = src; |
---|
[2298f728] | 545 | src = nullptr; |
---|
[101e0bd] | 546 | break; |
---|
| 547 | case TypeData::Basic: |
---|
| 548 | dst->qualifiers |= src->qualifiers; |
---|
| 549 | if ( src->kind != TypeData::Unknown ) { |
---|
| 550 | assert( src->kind == TypeData::Basic ); |
---|
| 551 | |
---|
| 552 | if ( dst->basictype == DeclarationNode::NoBasicType ) { |
---|
| 553 | dst->basictype = src->basictype; |
---|
| 554 | } else if ( src->basictype != DeclarationNode::NoBasicType ) |
---|
[fb114fa1] | 555 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src ); |
---|
[101e0bd] | 556 | |
---|
| 557 | if ( dst->complextype == DeclarationNode::NoComplexType ) { |
---|
| 558 | dst->complextype = src->complextype; |
---|
| 559 | } else if ( src->complextype != DeclarationNode::NoComplexType ) |
---|
[fb114fa1] | 560 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src ); |
---|
[101e0bd] | 561 | |
---|
| 562 | if ( dst->signedness == DeclarationNode::NoSignedness ) { |
---|
| 563 | dst->signedness = src->signedness; |
---|
| 564 | } else if ( src->signedness != DeclarationNode::NoSignedness ) |
---|
[fb114fa1] | 565 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src ); |
---|
[101e0bd] | 566 | |
---|
| 567 | if ( dst->length == DeclarationNode::NoLength ) { |
---|
| 568 | dst->length = src->length; |
---|
| 569 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { |
---|
| 570 | dst->length = DeclarationNode::LongLong; |
---|
| 571 | } else if ( src->length != DeclarationNode::NoLength ) |
---|
[fb114fa1] | 572 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src ); |
---|
[101e0bd] | 573 | } // if |
---|
| 574 | break; |
---|
| 575 | default: |
---|
| 576 | switch ( src->kind ) { |
---|
| 577 | case TypeData::Aggregate: |
---|
| 578 | case TypeData::Enum: |
---|
| 579 | dst->base = new TypeData( TypeData::AggregateInst ); |
---|
| 580 | dst->base->aggInst.aggregate = src; |
---|
| 581 | if ( src->kind == TypeData::Aggregate ) { |
---|
| 582 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); |
---|
[68cd1ce] | 583 | } // if |
---|
[101e0bd] | 584 | dst->base->qualifiers |= src->qualifiers; |
---|
[2298f728] | 585 | src = nullptr; |
---|
[b87a5ed] | 586 | break; |
---|
| 587 | default: |
---|
[101e0bd] | 588 | if ( dst->forall ) { |
---|
| 589 | dst->forall->appendList( src->forall ); |
---|
| 590 | } else { |
---|
| 591 | dst->forall = src->forall; |
---|
| 592 | } // if |
---|
[2298f728] | 593 | src->forall = nullptr; |
---|
[101e0bd] | 594 | dst->base = src; |
---|
[2298f728] | 595 | src = nullptr; |
---|
[68cd1ce] | 596 | } // switch |
---|
[101e0bd] | 597 | } // switch |
---|
[68cd1ce] | 598 | } // if |
---|
[3848e0e] | 599 | } |
---|
| 600 | |
---|
[ba7aa2d] | 601 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) { |
---|
[b87a5ed] | 602 | if ( o ) { |
---|
[b6424d9] | 603 | checkStorageClasses( o ); |
---|
[13e3b50] | 604 | copyStorageClasses( o ); |
---|
[b87a5ed] | 605 | if ( o->type ) { |
---|
| 606 | if ( ! type ) { |
---|
| 607 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { |
---|
| 608 | type = new TypeData( TypeData::AggregateInst ); |
---|
[8f6f47d7] | 609 | type->aggInst.aggregate = o->type; |
---|
[b87a5ed] | 610 | if ( o->type->kind == TypeData::Aggregate ) { |
---|
[8f6f47d7] | 611 | type->aggInst.params = maybeClone( o->type->aggregate.actuals ); |
---|
[68cd1ce] | 612 | } // if |
---|
[c1c1112] | 613 | type->qualifiers |= o->type->qualifiers; |
---|
[b87a5ed] | 614 | } else { |
---|
| 615 | type = o->type; |
---|
[68cd1ce] | 616 | } // if |
---|
[2298f728] | 617 | o->type = nullptr; |
---|
[b87a5ed] | 618 | } else { |
---|
| 619 | addTypeToType( o->type, type ); |
---|
[68cd1ce] | 620 | } // if |
---|
| 621 | } // if |
---|
[b87a5ed] | 622 | if ( o->bitfieldWidth ) { |
---|
| 623 | bitfieldWidth = o->bitfieldWidth; |
---|
[68cd1ce] | 624 | } // if |
---|
[71bd8c6] | 625 | |
---|
| 626 | // there may be typedefs chained onto the type |
---|
[1d4580a] | 627 | if ( o->get_next() ) { |
---|
| 628 | set_last( o->get_next()->clone() ); |
---|
[1db21619] | 629 | } // if |
---|
[68cd1ce] | 630 | } // if |
---|
[b87a5ed] | 631 | delete o; |
---|
| 632 | return this; |
---|
[3848e0e] | 633 | } |
---|
| 634 | |
---|
[ba7aa2d] | 635 | DeclarationNode * DeclarationNode::addTypedef() { |
---|
| 636 | TypeData * newtype = new TypeData( TypeData::Symbolic ); |
---|
[2298f728] | 637 | newtype->symbolic.params = nullptr; |
---|
[8f6f47d7] | 638 | newtype->symbolic.isTypedef = true; |
---|
[2298f728] | 639 | newtype->symbolic.name = name ? new string( *name ) : nullptr; |
---|
[b87a5ed] | 640 | newtype->base = type; |
---|
| 641 | type = newtype; |
---|
| 642 | return this; |
---|
[3848e0e] | 643 | } |
---|
| 644 | |
---|
[ba7aa2d] | 645 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { |
---|
[faddbd8] | 646 | if ( variable.tyClass != NoTypeClass ) { |
---|
[2298f728] | 647 | if ( variable.assertions ) { |
---|
| 648 | variable.assertions->appendList( assertions ); |
---|
| 649 | } else { |
---|
| 650 | variable.assertions = assertions; |
---|
| 651 | } // if |
---|
| 652 | return this; |
---|
| 653 | } // if |
---|
| 654 | |
---|
[b87a5ed] | 655 | assert( type ); |
---|
| 656 | switch ( type->kind ) { |
---|
| 657 | case TypeData::Symbolic: |
---|
[8f6f47d7] | 658 | if ( type->symbolic.assertions ) { |
---|
| 659 | type->symbolic.assertions->appendList( assertions ); |
---|
[b87a5ed] | 660 | } else { |
---|
[8f6f47d7] | 661 | type->symbolic.assertions = assertions; |
---|
[68cd1ce] | 662 | } // if |
---|
[b87a5ed] | 663 | break; |
---|
| 664 | default: |
---|
| 665 | assert( false ); |
---|
[68cd1ce] | 666 | } // switch |
---|
[974906e2] | 667 | |
---|
[b87a5ed] | 668 | return this; |
---|
[51b7345] | 669 | } |
---|
| 670 | |
---|
[fb114fa1] | 671 | DeclarationNode * DeclarationNode::addName( string * newname ) { |
---|
[2298f728] | 672 | assert( ! name ); |
---|
| 673 | name = newname; |
---|
[b87a5ed] | 674 | return this; |
---|
[51b7345] | 675 | } |
---|
| 676 | |
---|
[c0aa336] | 677 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) { |
---|
[58dd019] | 678 | assert( ! asmName ); |
---|
[c0aa336] | 679 | asmName = newname ? newname->asmName : nullptr; |
---|
| 680 | return this->addQualifiers( newname ); |
---|
[58dd019] | 681 | } |
---|
| 682 | |
---|
[ba7aa2d] | 683 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { |
---|
[b87a5ed] | 684 | bitfieldWidth = size; |
---|
| 685 | return this; |
---|
[51b7345] | 686 | } |
---|
| 687 | |
---|
[ba7aa2d] | 688 | DeclarationNode * DeclarationNode::addVarArgs() { |
---|
[b87a5ed] | 689 | assert( type ); |
---|
| 690 | hasEllipsis = true; |
---|
| 691 | return this; |
---|
[51b7345] | 692 | } |
---|
| 693 | |
---|
[ba7aa2d] | 694 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) { |
---|
[b87a5ed] | 695 | assert( type ); |
---|
| 696 | assert( type->kind == TypeData::Function ); |
---|
[2298f728] | 697 | assert( ! type->function.body ); |
---|
[8f6f47d7] | 698 | type->function.body = body; |
---|
| 699 | type->function.hasBody = true; |
---|
[b87a5ed] | 700 | return this; |
---|
[51b7345] | 701 | } |
---|
| 702 | |
---|
[ba7aa2d] | 703 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) { |
---|
[b87a5ed] | 704 | assert( type ); |
---|
| 705 | assert( type->kind == TypeData::Function ); |
---|
[2298f728] | 706 | assert( ! type->function.oldDeclList ); |
---|
[8f6f47d7] | 707 | type->function.oldDeclList = list; |
---|
[b87a5ed] | 708 | return this; |
---|
[51b7345] | 709 | } |
---|
| 710 | |
---|
[c0aa336] | 711 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { |
---|
[b87a5ed] | 712 | if ( type ) { |
---|
[ba7aa2d] | 713 | TypeData * prevBase = type; |
---|
| 714 | TypeData * curBase = type->base; |
---|
[2298f728] | 715 | while ( curBase != nullptr ) { |
---|
[b87a5ed] | 716 | prevBase = curBase; |
---|
| 717 | curBase = curBase->base; |
---|
[68cd1ce] | 718 | } // while |
---|
[b87a5ed] | 719 | prevBase->base = newType; |
---|
| 720 | } else { |
---|
| 721 | type = newType; |
---|
[68cd1ce] | 722 | } // if |
---|
[c0aa336] | 723 | return this; |
---|
[3848e0e] | 724 | } |
---|
| 725 | |
---|
[c0aa336] | 726 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) { |
---|
| 727 | if ( a ) { |
---|
| 728 | for ( Attribute *attr: reverseIterate( a->attributes ) ) { |
---|
| 729 | attributes.push_front( attr ); |
---|
| 730 | } // for |
---|
| 731 | a->attributes.clear(); |
---|
| 732 | } // if |
---|
| 733 | return this; |
---|
| 734 | } // copyAttribute |
---|
| 735 | |
---|
[ba7aa2d] | 736 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) { |
---|
[b87a5ed] | 737 | if ( p ) { |
---|
| 738 | assert( p->type->kind == TypeData::Pointer ); |
---|
[c0aa336] | 739 | setBase( p->type ); |
---|
[2298f728] | 740 | p->type = nullptr; |
---|
[c0aa336] | 741 | copyAttribute( p ); |
---|
[b87a5ed] | 742 | delete p; |
---|
[68cd1ce] | 743 | } // if |
---|
[b87a5ed] | 744 | return this; |
---|
[3848e0e] | 745 | } |
---|
| 746 | |
---|
[ba7aa2d] | 747 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) { |
---|
[b87a5ed] | 748 | if ( a ) { |
---|
| 749 | assert( a->type->kind == TypeData::Array ); |
---|
[c0aa336] | 750 | setBase( a->type ); |
---|
[2298f728] | 751 | a->type = nullptr; |
---|
[c0aa336] | 752 | copyAttribute( a ); |
---|
[b87a5ed] | 753 | delete a; |
---|
[68cd1ce] | 754 | } // if |
---|
[b87a5ed] | 755 | return this; |
---|
[51b7345] | 756 | } |
---|
| 757 | |
---|
[ba7aa2d] | 758 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) { |
---|
[b87a5ed] | 759 | if ( p ) { |
---|
| 760 | assert( p->type->kind == TypeData::Pointer ); |
---|
| 761 | if ( type ) { |
---|
| 762 | switch ( type->kind ) { |
---|
| 763 | case TypeData::Aggregate: |
---|
| 764 | case TypeData::Enum: |
---|
| 765 | p->type->base = new TypeData( TypeData::AggregateInst ); |
---|
[8f6f47d7] | 766 | p->type->base->aggInst.aggregate = type; |
---|
[b87a5ed] | 767 | if ( type->kind == TypeData::Aggregate ) { |
---|
[8f6f47d7] | 768 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); |
---|
[68cd1ce] | 769 | } // if |
---|
[c1c1112] | 770 | p->type->base->qualifiers |= type->qualifiers; |
---|
[b87a5ed] | 771 | break; |
---|
| 772 | |
---|
| 773 | default: |
---|
| 774 | p->type->base = type; |
---|
[68cd1ce] | 775 | } // switch |
---|
[2298f728] | 776 | type = nullptr; |
---|
[68cd1ce] | 777 | } // if |
---|
[b87a5ed] | 778 | delete this; |
---|
| 779 | return p; |
---|
| 780 | } else { |
---|
| 781 | return this; |
---|
[68cd1ce] | 782 | } // if |
---|
[51b7345] | 783 | } |
---|
| 784 | |
---|
[ba7aa2d] | 785 | static TypeData * findLast( TypeData * a ) { |
---|
[b87a5ed] | 786 | assert( a ); |
---|
[ba7aa2d] | 787 | TypeData * cur = a; |
---|
[a32b204] | 788 | while ( cur->base ) { |
---|
[b87a5ed] | 789 | cur = cur->base; |
---|
[68cd1ce] | 790 | } // while |
---|
[b87a5ed] | 791 | return cur; |
---|
[3848e0e] | 792 | } |
---|
| 793 | |
---|
[ba7aa2d] | 794 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { |
---|
[b87a5ed] | 795 | if ( a ) { |
---|
| 796 | assert( a->type->kind == TypeData::Array ); |
---|
[ba7aa2d] | 797 | TypeData * lastArray = findLast( a->type ); |
---|
[974906e2] | 798 | if ( type ) { |
---|
[b87a5ed] | 799 | switch ( type->kind ) { |
---|
| 800 | case TypeData::Aggregate: |
---|
| 801 | case TypeData::Enum: |
---|
| 802 | lastArray->base = new TypeData( TypeData::AggregateInst ); |
---|
[8f6f47d7] | 803 | lastArray->base->aggInst.aggregate = type; |
---|
[b87a5ed] | 804 | if ( type->kind == TypeData::Aggregate ) { |
---|
[8f6f47d7] | 805 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); |
---|
[68cd1ce] | 806 | } // if |
---|
[c1c1112] | 807 | lastArray->base->qualifiers |= type->qualifiers; |
---|
[b87a5ed] | 808 | break; |
---|
| 809 | default: |
---|
| 810 | lastArray->base = type; |
---|
[68cd1ce] | 811 | } // switch |
---|
[2298f728] | 812 | type = nullptr; |
---|
[68cd1ce] | 813 | } // if |
---|
[b87a5ed] | 814 | delete this; |
---|
| 815 | return a; |
---|
| 816 | } else { |
---|
| 817 | return this; |
---|
[68cd1ce] | 818 | } // if |
---|
[51b7345] | 819 | } |
---|
[3848e0e] | 820 | |
---|
[ba7aa2d] | 821 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) { |
---|
| 822 | TypeData * ftype = new TypeData( TypeData::Function ); |
---|
[8f6f47d7] | 823 | ftype->function.params = params; |
---|
[c0aa336] | 824 | setBase( ftype ); |
---|
[b87a5ed] | 825 | return this; |
---|
[3848e0e] | 826 | } |
---|
| 827 | |
---|
[ba7aa2d] | 828 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) { |
---|
[b87a5ed] | 829 | if ( type ) { |
---|
| 830 | if ( type->kind != TypeData::Function ) { |
---|
| 831 | type->base = addIdListToType( type->base, ids ); |
---|
| 832 | } else { |
---|
[8f6f47d7] | 833 | type->function.idList = ids; |
---|
[68cd1ce] | 834 | } // if |
---|
[b87a5ed] | 835 | return type; |
---|
[3848e0e] | 836 | } else { |
---|
[ba7aa2d] | 837 | TypeData * newtype = new TypeData( TypeData::Function ); |
---|
[8f6f47d7] | 838 | newtype->function.idList = ids; |
---|
[b87a5ed] | 839 | return newtype; |
---|
[68cd1ce] | 840 | } // if |
---|
[2298f728] | 841 | } // addIdListToType |
---|
[974906e2] | 842 | |
---|
[ba7aa2d] | 843 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) { |
---|
[b87a5ed] | 844 | type = addIdListToType( type, ids ); |
---|
| 845 | return this; |
---|
[3848e0e] | 846 | } |
---|
| 847 | |
---|
[ba7aa2d] | 848 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { |
---|
[b87a5ed] | 849 | initializer = init; |
---|
| 850 | return this; |
---|
[3848e0e] | 851 | } |
---|
| 852 | |
---|
[ba7aa2d] | 853 | DeclarationNode * DeclarationNode::cloneType( string * newName ) { |
---|
| 854 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 855 | newnode->type = maybeClone( type ); |
---|
[b6424d9] | 856 | assert( storageClass == NoStorageClass ); |
---|
[13e3b50] | 857 | newnode->copyStorageClasses( this ); |
---|
[ba7aa2d] | 858 | assert( newName ); |
---|
[2298f728] | 859 | newnode->name = newName; |
---|
[b87a5ed] | 860 | return newnode; |
---|
[3848e0e] | 861 | } |
---|
| 862 | |
---|
[2298f728] | 863 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) { |
---|
| 864 | if ( ! o ) return nullptr; |
---|
| 865 | |
---|
| 866 | o->copyStorageClasses( this ); |
---|
| 867 | if ( type ) { |
---|
| 868 | TypeData * srcType = type; |
---|
| 869 | |
---|
[c0aa336] | 870 | // search for the base type by scanning off pointers and array designators |
---|
[2298f728] | 871 | while ( srcType->base ) { |
---|
| 872 | srcType = srcType->base; |
---|
| 873 | } // while |
---|
| 874 | |
---|
| 875 | TypeData * newType = srcType->clone(); |
---|
| 876 | if ( newType->kind == TypeData::AggregateInst ) { |
---|
| 877 | // don't duplicate members |
---|
| 878 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { |
---|
| 879 | delete newType->aggInst.aggregate->enumeration.constants; |
---|
| 880 | newType->aggInst.aggregate->enumeration.constants = nullptr; |
---|
[b87a5ed] | 881 | } else { |
---|
[2298f728] | 882 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); |
---|
| 883 | delete newType->aggInst.aggregate->aggregate.fields; |
---|
| 884 | newType->aggInst.aggregate->aggregate.fields = nullptr; |
---|
[68cd1ce] | 885 | } // if |
---|
| 886 | } // if |
---|
[2298f728] | 887 | |
---|
| 888 | newType->forall = maybeClone( type->forall ); |
---|
| 889 | if ( ! o->type ) { |
---|
| 890 | o->type = newType; |
---|
| 891 | } else { |
---|
| 892 | addTypeToType( newType, o->type ); |
---|
| 893 | delete newType; |
---|
| 894 | } // if |
---|
[68cd1ce] | 895 | } // if |
---|
[b87a5ed] | 896 | return o; |
---|
[51b7345] | 897 | } |
---|
| 898 | |
---|
[ba7aa2d] | 899 | DeclarationNode * DeclarationNode::extractAggregate() const { |
---|
[b87a5ed] | 900 | if ( type ) { |
---|
[ba7aa2d] | 901 | TypeData * ret = typeextractAggregate( type ); |
---|
[b87a5ed] | 902 | if ( ret ) { |
---|
[ba7aa2d] | 903 | DeclarationNode * newnode = new DeclarationNode; |
---|
[b87a5ed] | 904 | newnode->type = ret; |
---|
| 905 | return newnode; |
---|
[843054c2] | 906 | } // if |
---|
| 907 | } // if |
---|
[2298f728] | 908 | return nullptr; |
---|
[3848e0e] | 909 | } |
---|
| 910 | |
---|
[ba7aa2d] | 911 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) { |
---|
[b87a5ed] | 912 | SemanticError errors; |
---|
[7880579] | 913 | std::back_insert_iterator< std::list< Declaration * > > out( outputList ); |
---|
[ba7aa2d] | 914 | const DeclarationNode * cur = firstNode; |
---|
[2298f728] | 915 | |
---|
[a32b204] | 916 | while ( cur ) { |
---|
[b87a5ed] | 917 | try { |
---|
[ba7aa2d] | 918 | if ( DeclarationNode * extr = cur->extractAggregate() ) { |
---|
[b87a5ed] | 919 | // handle the case where a structure declaration is contained within an object or type declaration |
---|
[ba7aa2d] | 920 | Declaration * decl = extr->build(); |
---|
[b87a5ed] | 921 | if ( decl ) { |
---|
[ba7aa2d] | 922 | * out++ = decl; |
---|
[843054c2] | 923 | } // if |
---|
[f39096c] | 924 | delete extr; |
---|
[843054c2] | 925 | } // if |
---|
[2298f728] | 926 | |
---|
[ba7aa2d] | 927 | Declaration * decl = cur->build(); |
---|
[b87a5ed] | 928 | if ( decl ) { |
---|
[ba7aa2d] | 929 | * out++ = decl; |
---|
[843054c2] | 930 | } // if |
---|
[b87a5ed] | 931 | } catch( SemanticError &e ) { |
---|
| 932 | errors.append( e ); |
---|
[843054c2] | 933 | } // try |
---|
[7880579] | 934 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); |
---|
[843054c2] | 935 | } // while |
---|
[2298f728] | 936 | |
---|
[b87a5ed] | 937 | if ( ! errors.isEmpty() ) { |
---|
| 938 | throw errors; |
---|
[843054c2] | 939 | } // if |
---|
[2298f728] | 940 | } // buildList |
---|
[3848e0e] | 941 | |
---|
[ba7aa2d] | 942 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) { |
---|
[b87a5ed] | 943 | SemanticError errors; |
---|
[7880579] | 944 | std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); |
---|
[ba7aa2d] | 945 | const DeclarationNode * cur = firstNode; |
---|
[a32b204] | 946 | while ( cur ) { |
---|
[b87a5ed] | 947 | try { |
---|
[ba7aa2d] | 948 | Declaration * decl = cur->build(); |
---|
[b87a5ed] | 949 | if ( decl ) { |
---|
[ba7aa2d] | 950 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
| 951 | * out++ = dwt; |
---|
| 952 | } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { |
---|
| 953 | StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); |
---|
[2298f728] | 954 | * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); |
---|
[b87a5ed] | 955 | delete agg; |
---|
[ba7aa2d] | 956 | } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { |
---|
| 957 | UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); |
---|
[2298f728] | 958 | * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr ); |
---|
[843054c2] | 959 | } // if |
---|
| 960 | } // if |
---|
[b87a5ed] | 961 | } catch( SemanticError &e ) { |
---|
| 962 | errors.append( e ); |
---|
[843054c2] | 963 | } // try |
---|
[7880579] | 964 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); |
---|
[843054c2] | 965 | } // while |
---|
[b87a5ed] | 966 | if ( ! errors.isEmpty() ) { |
---|
| 967 | throw errors; |
---|
[843054c2] | 968 | } // if |
---|
[2298f728] | 969 | } // buildList |
---|
[3848e0e] | 970 | |
---|
[ba7aa2d] | 971 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) { |
---|
[b87a5ed] | 972 | SemanticError errors; |
---|
[7880579] | 973 | std::back_insert_iterator< std::list< Type * > > out( outputList ); |
---|
[ba7aa2d] | 974 | const DeclarationNode * cur = firstNode; |
---|
[2298f728] | 975 | |
---|
[a32b204] | 976 | while ( cur ) { |
---|
[b87a5ed] | 977 | try { |
---|
[ba7aa2d] | 978 | * out++ = cur->buildType(); |
---|
[b87a5ed] | 979 | } catch( SemanticError &e ) { |
---|
| 980 | errors.append( e ); |
---|
[843054c2] | 981 | } // try |
---|
[7880579] | 982 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); |
---|
[843054c2] | 983 | } // while |
---|
[2298f728] | 984 | |
---|
[b87a5ed] | 985 | if ( ! errors.isEmpty() ) { |
---|
| 986 | throw errors; |
---|
[843054c2] | 987 | } // if |
---|
[2298f728] | 988 | } // buildTypeList |
---|
[51b7345] | 989 | |
---|
[ba7aa2d] | 990 | Declaration * DeclarationNode::build() const { |
---|
[7d05e7e] | 991 | if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this ); |
---|
[2298f728] | 992 | |
---|
[e994912] | 993 | if ( asmStmt ) { |
---|
| 994 | return new AsmDecl( safe_dynamic_cast<AsmStmt *>( asmStmt->build() ) ); |
---|
| 995 | } // if |
---|
| 996 | |
---|
[faddbd8] | 997 | // if ( variable.name ) { |
---|
| 998 | if ( variable.tyClass != NoTypeClass ) { |
---|
[8f60f0b] | 999 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype }; |
---|
| 1000 | assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." ); |
---|
| 1001 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); |
---|
[faddbd8] | 1002 | // TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); |
---|
| 1003 | TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] ); |
---|
[2298f728] | 1004 | buildList( variable.assertions, ret->get_assertions() ); |
---|
| 1005 | return ret; |
---|
| 1006 | } // if |
---|
| 1007 | |
---|
[843054c2] | 1008 | if ( type ) { |
---|
[44a81853] | 1009 | return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); |
---|
[843054c2] | 1010 | } // if |
---|
[2298f728] | 1011 | |
---|
[13e3b50] | 1012 | if ( ! isInline && ! isNoreturn ) { |
---|
[2298f728] | 1013 | assertf( name, "ObjectDecl are assumed to have names\n" ); |
---|
[58dd019] | 1014 | return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); |
---|
[843054c2] | 1015 | } // if |
---|
[2298f728] | 1016 | |
---|
[7d05e7e] | 1017 | throw SemanticError( "invalid function specifier ", this ); |
---|
[51b7345] | 1018 | } |
---|
| 1019 | |
---|
[ba7aa2d] | 1020 | Type * DeclarationNode::buildType() const { |
---|
[b87a5ed] | 1021 | assert( type ); |
---|
[974906e2] | 1022 | |
---|
[faddbd8] | 1023 | if ( attr.expr ) { |
---|
| 1024 | // return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() ); |
---|
[c0aa336] | 1025 | return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes ); |
---|
[faddbd8] | 1026 | } else if ( attr.type ) { |
---|
| 1027 | // return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() ); |
---|
[c0aa336] | 1028 | return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes ); |
---|
[2298f728] | 1029 | } // if |
---|
| 1030 | |
---|
[b87a5ed] | 1031 | switch ( type->kind ) { |
---|
[c0aa336] | 1032 | case TypeData::Enum: { |
---|
| 1033 | EnumDecl * typedecl = buildEnum( type, attributes ); |
---|
| 1034 | return new EnumInstType( buildQualifiers( type ), typedecl ); |
---|
| 1035 | } |
---|
[b87a5ed] | 1036 | case TypeData::Aggregate: { |
---|
[c0aa336] | 1037 | AggregateDecl * typedecl = buildAggregate( type, attributes ); |
---|
[ba7aa2d] | 1038 | ReferenceToType * ret; |
---|
[8f6f47d7] | 1039 | switch ( type->aggregate.kind ) { |
---|
[b87a5ed] | 1040 | case DeclarationNode::Struct: |
---|
[c0aa336] | 1041 | ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl ); |
---|
[b87a5ed] | 1042 | break; |
---|
| 1043 | case DeclarationNode::Union: |
---|
[c0aa336] | 1044 | ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl ); |
---|
[b87a5ed] | 1045 | break; |
---|
[4040425] | 1046 | case DeclarationNode::Trait: |
---|
[c0aa336] | 1047 | assert( false ); |
---|
| 1048 | //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl ); |
---|
[b87a5ed] | 1049 | break; |
---|
| 1050 | default: |
---|
| 1051 | assert( false ); |
---|
| 1052 | } // switch |
---|
[8f6f47d7] | 1053 | buildList( type->aggregate.actuals, ret->get_parameters() ); |
---|
[b87a5ed] | 1054 | return ret; |
---|
| 1055 | } |
---|
| 1056 | case TypeData::Symbolic: { |
---|
[c0aa336] | 1057 | TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes ); |
---|
[8f6f47d7] | 1058 | buildList( type->symbolic.actuals, ret->get_parameters() ); |
---|
[b87a5ed] | 1059 | return ret; |
---|
| 1060 | } |
---|
| 1061 | default: |
---|
[c0aa336] | 1062 | Type * simpletypes = typebuild( type ); |
---|
| 1063 | simpletypes->get_attributes() = attributes; // copy because member is const |
---|
| 1064 | return simpletypes; |
---|
[b87a5ed] | 1065 | } // switch |
---|
[3848e0e] | 1066 | } |
---|
| 1067 | |
---|
[b87a5ed] | 1068 | // Local Variables: // |
---|
| 1069 | // tab-width: 4 // |
---|
| 1070 | // mode: c++ // |
---|
| 1071 | // compile-command: "make install" // |
---|
| 1072 | // End: // |
---|