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