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