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