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