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