[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 | // |
---|
| 7 | // DeclarationNode.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 12:34:05 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[843054c2] | 12 | // Last Modified On : Thu May 21 09:28:54 2015 |
---|
| 13 | // Update Count : 13 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <string> |
---|
| 17 | #include <list> |
---|
| 18 | #include <iterator> |
---|
| 19 | #include <algorithm> |
---|
| 20 | #include <cassert> |
---|
| 21 | |
---|
| 22 | #include "TypeData.h" |
---|
| 23 | #include "SynTree/Expression.h" |
---|
[bdd516a] | 24 | |
---|
[51b7345] | 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
[bdd516a] | 28 | // These must remain in the same order as the corresponding DeclarationNode enumerations. |
---|
| 29 | const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" }; |
---|
[17cd4eb] | 30 | const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" }; |
---|
[51b7345] | 31 | const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" }; |
---|
| 32 | const char *DeclarationNode::tyConName[] = { "struct", "union", "context" }; |
---|
| 33 | const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" }; |
---|
| 34 | |
---|
| 35 | UniqueName DeclarationNode::anonymous( "__anonymous" ); |
---|
| 36 | |
---|
| 37 | extern LinkageSpec::Type linkage; /* defined in cfa.y */ |
---|
| 38 | |
---|
[3848e0e] | 39 | DeclarationNode *DeclarationNode::clone() const { |
---|
[b87a5ed] | 40 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 41 | newnode->type = maybeClone( type ); |
---|
| 42 | newnode->name = name; |
---|
| 43 | newnode->storageClasses = storageClasses; |
---|
| 44 | newnode->bitfieldWidth = maybeClone( bitfieldWidth ); |
---|
| 45 | newnode->hasEllipsis = hasEllipsis; |
---|
| 46 | newnode->initializer = initializer; |
---|
| 47 | newnode->next = maybeClone( next ); |
---|
| 48 | newnode->linkage = linkage; |
---|
| 49 | return newnode; |
---|
[3848e0e] | 50 | } |
---|
| 51 | |
---|
| 52 | DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | DeclarationNode::~DeclarationNode() { |
---|
[b87a5ed] | 56 | delete type; |
---|
| 57 | delete bitfieldWidth; |
---|
| 58 | delete initializer; |
---|
[3848e0e] | 59 | } |
---|
| 60 | |
---|
| 61 | bool DeclarationNode::get_hasEllipsis() const { |
---|
[b87a5ed] | 62 | return hasEllipsis; |
---|
[3848e0e] | 63 | } |
---|
| 64 | |
---|
| 65 | const char *storageClassName[] = { |
---|
[b87a5ed] | 66 | // order must correspond with DeclarationNode::StorageClass |
---|
| 67 | "extern", |
---|
| 68 | "static", |
---|
| 69 | "auto", |
---|
| 70 | "register", |
---|
| 71 | "inline", |
---|
| 72 | "fortran", |
---|
[51b7345] | 73 | }; |
---|
| 74 | |
---|
[3848e0e] | 75 | void DeclarationNode::print( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 76 | os << string(indent, ' ' ); |
---|
| 77 | if ( name == "" ) { |
---|
| 78 | os << "unnamed: "; |
---|
| 79 | } else { |
---|
| 80 | os << name << ": "; |
---|
| 81 | } |
---|
[51b7345] | 82 | |
---|
[b87a5ed] | 83 | if ( linkage != LinkageSpec::Cforall ) { |
---|
| 84 | os << LinkageSpec::toString( linkage ) << " "; |
---|
| 85 | } |
---|
[3848e0e] | 86 | |
---|
[b87a5ed] | 87 | printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os ); |
---|
| 88 | if ( type ) { |
---|
| 89 | type->print( os, indent ); |
---|
| 90 | } else { |
---|
| 91 | os << "untyped entity "; |
---|
| 92 | } |
---|
[3848e0e] | 93 | |
---|
[b87a5ed] | 94 | if ( bitfieldWidth ) { |
---|
| 95 | os << endl << string(indent+2, ' ') << "with bitfield width "; |
---|
| 96 | bitfieldWidth->printOneLine( os ); |
---|
| 97 | } |
---|
[3848e0e] | 98 | |
---|
[b87a5ed] | 99 | if ( initializer != 0 ) { |
---|
| 100 | os << endl << string(indent+2, ' ') << "with initializer "; |
---|
| 101 | initializer->printOneLine( os ); |
---|
| 102 | } |
---|
[3848e0e] | 103 | |
---|
[b87a5ed] | 104 | os << endl; |
---|
[51b7345] | 105 | } |
---|
| 106 | |
---|
[3848e0e] | 107 | void DeclarationNode::printList( std::ostream &os, int indent ) const { |
---|
[b87a5ed] | 108 | ParseNode::printList( os, indent ); |
---|
| 109 | if ( hasEllipsis ) { |
---|
| 110 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; |
---|
| 111 | } |
---|
[51b7345] | 112 | } |
---|
| 113 | |
---|
[d9a0e76] | 114 | DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) { |
---|
[b87a5ed] | 115 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 116 | newnode->name = assign_strptr( name ); |
---|
[3848e0e] | 117 | |
---|
[b87a5ed] | 118 | newnode->type = new TypeData( TypeData::Function ); |
---|
| 119 | newnode->type->function->params = param; |
---|
| 120 | newnode->type->function->newStyle = newStyle; |
---|
| 121 | newnode->type->function->body = body; |
---|
[3848e0e] | 122 | |
---|
[b87a5ed] | 123 | if ( body ) { |
---|
| 124 | newnode->type->function->hasBody = true; |
---|
| 125 | } |
---|
[51b7345] | 126 | |
---|
[b87a5ed] | 127 | if ( ret ) { |
---|
| 128 | newnode->type->base = ret->type; |
---|
| 129 | ret->type = 0; |
---|
| 130 | delete ret; |
---|
| 131 | } |
---|
[51b7345] | 132 | |
---|
[b87a5ed] | 133 | return newnode; |
---|
[3848e0e] | 134 | } |
---|
| 135 | |
---|
| 136 | DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) { |
---|
[b87a5ed] | 137 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 138 | newnode->type = new TypeData(); |
---|
| 139 | newnode->type->qualifiers.push_back( q ); |
---|
| 140 | return newnode; |
---|
[3848e0e] | 141 | } |
---|
| 142 | |
---|
| 143 | DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) { |
---|
[b87a5ed] | 144 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 145 | newnode->storageClasses.push_back( sc ); |
---|
| 146 | return newnode; |
---|
[3848e0e] | 147 | } |
---|
| 148 | |
---|
| 149 | DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) { |
---|
[b87a5ed] | 150 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 151 | newnode->type = new TypeData( TypeData::Basic ); |
---|
| 152 | newnode->type->basic->typeSpec.push_back( bt ); |
---|
| 153 | return newnode; |
---|
[3848e0e] | 154 | } |
---|
| 155 | |
---|
| 156 | DeclarationNode *DeclarationNode::newModifier( Modifier mod ) { |
---|
[b87a5ed] | 157 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 158 | newnode->type = new TypeData( TypeData::Basic ); |
---|
| 159 | newnode->type->basic->modifiers.push_back( mod ); |
---|
| 160 | return newnode; |
---|
[3848e0e] | 161 | } |
---|
| 162 | |
---|
| 163 | DeclarationNode *DeclarationNode::newForall( DeclarationNode* forall ) { |
---|
[b87a5ed] | 164 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 165 | newnode->type = new TypeData( TypeData::Unknown ); |
---|
| 166 | newnode->type->forall = forall; |
---|
| 167 | return newnode; |
---|
[3848e0e] | 168 | } |
---|
| 169 | |
---|
| 170 | DeclarationNode *DeclarationNode::newFromTypedef( std::string* name ) { |
---|
[b87a5ed] | 171 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 172 | newnode->type = new TypeData( TypeData::SymbolicInst ); |
---|
| 173 | newnode->type->symbolic->name = assign_strptr( name ); |
---|
| 174 | newnode->type->symbolic->isTypedef = true; |
---|
| 175 | newnode->type->symbolic->params = 0; |
---|
| 176 | return newnode; |
---|
[3848e0e] | 177 | } |
---|
| 178 | |
---|
| 179 | DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string* name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) { |
---|
[b87a5ed] | 180 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 181 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
| 182 | newnode->type->aggregate->kind = kind; |
---|
| 183 | newnode->type->aggregate->name = assign_strptr( name ); |
---|
| 184 | if ( newnode->type->aggregate->name == "" ) { |
---|
| 185 | newnode->type->aggregate->name = DeclarationNode::anonymous.newName(); |
---|
| 186 | } |
---|
| 187 | newnode->type->aggregate->params = formals; |
---|
| 188 | newnode->type->aggregate->actuals = actuals; |
---|
| 189 | newnode->type->aggregate->members = fields; |
---|
| 190 | return newnode; |
---|
[3848e0e] | 191 | } |
---|
| 192 | |
---|
| 193 | DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) { |
---|
[b87a5ed] | 194 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 195 | newnode->name = assign_strptr( name ); |
---|
| 196 | newnode->type = new TypeData( TypeData::Enum ); |
---|
| 197 | newnode->type->enumeration->name = newnode->name; |
---|
| 198 | if ( newnode->type->enumeration->name == "" ) { |
---|
| 199 | newnode->type->enumeration->name = DeclarationNode::anonymous.newName(); |
---|
| 200 | } |
---|
| 201 | newnode->type->enumeration->constants = constants; |
---|
| 202 | return newnode; |
---|
[3848e0e] | 203 | } |
---|
| 204 | |
---|
| 205 | DeclarationNode *DeclarationNode::newEnumConstant( std::string* name, ExpressionNode *constant ) { |
---|
[b87a5ed] | 206 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 207 | newnode->name = assign_strptr( name ); |
---|
| 208 | // do something with the constant |
---|
| 209 | return newnode; |
---|
[3848e0e] | 210 | } |
---|
| 211 | |
---|
| 212 | DeclarationNode *DeclarationNode::newName( std::string* name ) { |
---|
[b87a5ed] | 213 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 214 | newnode->name = assign_strptr( name ); |
---|
| 215 | return newnode; |
---|
[3848e0e] | 216 | } |
---|
| 217 | |
---|
| 218 | DeclarationNode *DeclarationNode::newFromTypeGen( std::string* name, ExpressionNode *params ) { |
---|
[b87a5ed] | 219 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 220 | newnode->type = new TypeData( TypeData::SymbolicInst ); |
---|
| 221 | newnode->type->symbolic->name = assign_strptr( name ); |
---|
| 222 | newnode->type->symbolic->isTypedef = false; |
---|
| 223 | newnode->type->symbolic->actuals = params; |
---|
| 224 | return newnode; |
---|
[3848e0e] | 225 | } |
---|
| 226 | |
---|
| 227 | DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string* name ) { |
---|
[b87a5ed] | 228 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 229 | newnode->name = assign_strptr( name ); |
---|
| 230 | newnode->type = new TypeData( TypeData::Variable ); |
---|
| 231 | newnode->type->variable->tyClass = tc; |
---|
| 232 | newnode->type->variable->name = newnode->name; |
---|
| 233 | return newnode; |
---|
[3848e0e] | 234 | } |
---|
| 235 | |
---|
| 236 | DeclarationNode *DeclarationNode::newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) { |
---|
[b87a5ed] | 237 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 238 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
| 239 | newnode->type->aggregate->kind = Context; |
---|
| 240 | newnode->type->aggregate->params = params; |
---|
| 241 | newnode->type->aggregate->members = asserts; |
---|
| 242 | newnode->type->aggregate->name = assign_strptr( name ); |
---|
| 243 | return newnode; |
---|
[3848e0e] | 244 | } |
---|
| 245 | |
---|
| 246 | DeclarationNode *DeclarationNode::newContextUse( std::string *name, ExpressionNode *params ) { |
---|
[b87a5ed] | 247 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 248 | newnode->type = new TypeData( TypeData::AggregateInst ); |
---|
| 249 | newnode->type->aggInst->aggregate = new TypeData( TypeData::Aggregate ); |
---|
| 250 | newnode->type->aggInst->aggregate->aggregate->kind = Context; |
---|
| 251 | newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name ); |
---|
| 252 | newnode->type->aggInst->params = params; |
---|
| 253 | return newnode; |
---|
[3848e0e] | 254 | } |
---|
| 255 | |
---|
| 256 | DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) { |
---|
[b87a5ed] | 257 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 258 | newnode->name = assign_strptr( name ); |
---|
| 259 | newnode->type = new TypeData( TypeData::Symbolic ); |
---|
| 260 | newnode->type->symbolic->isTypedef = false; |
---|
| 261 | newnode->type->symbolic->params = typeParams; |
---|
| 262 | newnode->type->symbolic->name = newnode->name; |
---|
| 263 | return newnode; |
---|
[3848e0e] | 264 | } |
---|
| 265 | |
---|
| 266 | DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) { |
---|
[b87a5ed] | 267 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 268 | newnode->type = new TypeData( TypeData::Pointer ); |
---|
| 269 | return newnode->addQualifiers( qualifiers ); |
---|
[3848e0e] | 270 | } |
---|
| 271 | |
---|
| 272 | DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) { |
---|
[b87a5ed] | 273 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 274 | newnode->type = new TypeData( TypeData::Array ); |
---|
| 275 | newnode->type->array->dimension = size; |
---|
| 276 | newnode->type->array->isStatic = isStatic; |
---|
| 277 | newnode->type->array->isVarLen = false; |
---|
| 278 | return newnode->addQualifiers( qualifiers ); |
---|
[3848e0e] | 279 | } |
---|
| 280 | |
---|
| 281 | DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) { |
---|
[b87a5ed] | 282 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 283 | newnode->type = new TypeData( TypeData::Array ); |
---|
| 284 | newnode->type->array->dimension = 0; |
---|
| 285 | newnode->type->array->isStatic = false; |
---|
| 286 | newnode->type->array->isVarLen = true; |
---|
| 287 | return newnode->addQualifiers( qualifiers ); |
---|
[3848e0e] | 288 | } |
---|
| 289 | |
---|
| 290 | DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) { |
---|
[b87a5ed] | 291 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 292 | newnode->bitfieldWidth = size; |
---|
| 293 | return newnode; |
---|
[3848e0e] | 294 | } |
---|
| 295 | |
---|
| 296 | DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) { |
---|
[b87a5ed] | 297 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 298 | newnode->type = new TypeData( TypeData::Tuple ); |
---|
| 299 | newnode->type->tuple->members = members; |
---|
| 300 | return newnode; |
---|
[3848e0e] | 301 | } |
---|
| 302 | |
---|
| 303 | DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) { |
---|
[b87a5ed] | 304 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 305 | newnode->type = new TypeData( TypeData::Typeof ); |
---|
| 306 | newnode->type->typeexpr->expr = expr; |
---|
| 307 | return newnode; |
---|
[3848e0e] | 308 | } |
---|
| 309 | |
---|
| 310 | DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) { |
---|
[b87a5ed] | 311 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 312 | newnode->type = new TypeData( TypeData::Attr ); |
---|
| 313 | newnode->type->attr->name = assign_strptr( name ); |
---|
| 314 | newnode->type->attr->expr = expr; |
---|
| 315 | return newnode; |
---|
[3848e0e] | 316 | } |
---|
| 317 | |
---|
| 318 | DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) { |
---|
[b87a5ed] | 319 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 320 | newnode->type = new TypeData( TypeData::Attr ); |
---|
| 321 | newnode->type->attr->name = assign_strptr( name ); |
---|
| 322 | newnode->type->attr->type = type; |
---|
| 323 | return newnode; |
---|
[3848e0e] | 324 | } |
---|
| 325 | |
---|
| 326 | static void addQualifiersToType( TypeData *&src, TypeData *dst ) { |
---|
[b87a5ed] | 327 | if ( src && dst ) { |
---|
| 328 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
| 329 | if ( dst->forall ) { |
---|
| 330 | dst->forall->appendList( src->forall ); |
---|
| 331 | } else { |
---|
| 332 | dst->forall = src->forall; |
---|
| 333 | } |
---|
| 334 | src->forall = 0; |
---|
| 335 | } |
---|
| 336 | if ( dst->base ) { |
---|
| 337 | addQualifiersToType( src, dst->base ); |
---|
| 338 | } else if ( dst->kind == TypeData::Function ) { |
---|
| 339 | dst->base = src; |
---|
| 340 | src = 0; |
---|
| 341 | } else { |
---|
| 342 | dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers ); |
---|
| 343 | } |
---|
[51b7345] | 344 | } |
---|
[3848e0e] | 345 | } |
---|
[b87a5ed] | 346 | |
---|
[3848e0e] | 347 | DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) { |
---|
[b87a5ed] | 348 | if ( q ) { |
---|
| 349 | storageClasses.splice( storageClasses.end(), q->storageClasses ); |
---|
| 350 | if ( q->type ) { |
---|
| 351 | if ( ! type ) { |
---|
| 352 | type = new TypeData; |
---|
| 353 | } |
---|
| 354 | addQualifiersToType( q->type, type ); |
---|
| 355 | if ( q->type && q->type->forall ) { |
---|
| 356 | if ( type->forall ) { |
---|
| 357 | type->forall->appendList( q->type->forall ); |
---|
| 358 | } else { |
---|
| 359 | type->forall = q->type->forall; |
---|
| 360 | } |
---|
| 361 | q->type->forall = 0; |
---|
| 362 | } |
---|
[3848e0e] | 363 | } |
---|
| 364 | } |
---|
[b87a5ed] | 365 | delete q; |
---|
| 366 | return this; |
---|
[3848e0e] | 367 | } |
---|
| 368 | |
---|
| 369 | DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) { |
---|
[b87a5ed] | 370 | storageClasses = q->storageClasses; |
---|
| 371 | return this; |
---|
[3848e0e] | 372 | } |
---|
| 373 | |
---|
| 374 | static void addTypeToType( TypeData *&src, TypeData *&dst ) { |
---|
[b87a5ed] | 375 | if ( src && dst ) { |
---|
| 376 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
| 377 | if ( dst->forall ) { |
---|
| 378 | dst->forall->appendList( src->forall ); |
---|
| 379 | } else { |
---|
| 380 | dst->forall = src->forall; |
---|
| 381 | } |
---|
| 382 | src->forall = 0; |
---|
[3848e0e] | 383 | } |
---|
[b87a5ed] | 384 | if ( dst->base ) { |
---|
| 385 | addTypeToType( src, dst->base ); |
---|
| 386 | } else { |
---|
| 387 | switch ( dst->kind ) { |
---|
| 388 | case TypeData::Unknown: |
---|
| 389 | src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers ); |
---|
| 390 | dst = src; |
---|
| 391 | src = 0; |
---|
| 392 | break; |
---|
| 393 | |
---|
| 394 | case TypeData::Basic: |
---|
| 395 | dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers ); |
---|
| 396 | if ( src->kind != TypeData::Unknown ) { |
---|
| 397 | assert( src->kind == TypeData::Basic ); |
---|
| 398 | dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers ); |
---|
| 399 | dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec ); |
---|
| 400 | } |
---|
| 401 | break; |
---|
| 402 | |
---|
| 403 | default: |
---|
| 404 | switch ( src->kind ) { |
---|
| 405 | case TypeData::Aggregate: |
---|
| 406 | case TypeData::Enum: |
---|
| 407 | dst->base = new TypeData( TypeData::AggregateInst ); |
---|
| 408 | dst->base->aggInst->aggregate = src; |
---|
| 409 | if ( src->kind == TypeData::Aggregate ) { |
---|
| 410 | dst->base->aggInst->params = maybeClone( src->aggregate->actuals ); |
---|
| 411 | } |
---|
| 412 | dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers ); |
---|
| 413 | src = 0; |
---|
| 414 | break; |
---|
| 415 | |
---|
| 416 | default: |
---|
| 417 | if ( dst->forall ) { |
---|
| 418 | dst->forall->appendList( src->forall ); |
---|
| 419 | } else { |
---|
| 420 | dst->forall = src->forall; |
---|
| 421 | } |
---|
| 422 | src->forall = 0; |
---|
| 423 | dst->base = src; |
---|
| 424 | src = 0; |
---|
| 425 | } |
---|
| 426 | } |
---|
[3848e0e] | 427 | } |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) { |
---|
[b87a5ed] | 432 | if ( o ) { |
---|
| 433 | storageClasses.splice( storageClasses.end(), o->storageClasses ); |
---|
| 434 | if ( o->type ) { |
---|
| 435 | if ( ! type ) { |
---|
| 436 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { |
---|
| 437 | type = new TypeData( TypeData::AggregateInst ); |
---|
| 438 | type->aggInst->aggregate = o->type; |
---|
| 439 | if ( o->type->kind == TypeData::Aggregate ) { |
---|
| 440 | type->aggInst->params = maybeClone( o->type->aggregate->actuals ); |
---|
| 441 | } |
---|
| 442 | type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers ); |
---|
| 443 | } else { |
---|
| 444 | type = o->type; |
---|
| 445 | } |
---|
| 446 | o->type = 0; |
---|
| 447 | } else { |
---|
| 448 | addTypeToType( o->type, type ); |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | if ( o->bitfieldWidth ) { |
---|
| 452 | bitfieldWidth = o->bitfieldWidth; |
---|
[3848e0e] | 453 | } |
---|
| 454 | } |
---|
[b87a5ed] | 455 | delete o; |
---|
| 456 | return this; |
---|
[3848e0e] | 457 | } |
---|
| 458 | |
---|
| 459 | DeclarationNode *DeclarationNode::addTypedef() { |
---|
[b87a5ed] | 460 | TypeData *newtype = new TypeData( TypeData::Symbolic ); |
---|
| 461 | newtype->symbolic->params = 0; |
---|
| 462 | newtype->symbolic->isTypedef = true; |
---|
| 463 | newtype->symbolic->name = name; |
---|
| 464 | newtype->base = type; |
---|
| 465 | type = newtype; |
---|
| 466 | return this; |
---|
[3848e0e] | 467 | } |
---|
| 468 | |
---|
| 469 | DeclarationNode *DeclarationNode::addAssertions( DeclarationNode* assertions ) { |
---|
[b87a5ed] | 470 | assert( type ); |
---|
| 471 | switch ( type->kind ) { |
---|
| 472 | case TypeData::Symbolic: |
---|
| 473 | if ( type->symbolic->assertions ) { |
---|
| 474 | type->symbolic->assertions->appendList( assertions ); |
---|
| 475 | } else { |
---|
| 476 | type->symbolic->assertions = assertions; |
---|
| 477 | } |
---|
| 478 | break; |
---|
| 479 | |
---|
| 480 | case TypeData::Variable: |
---|
| 481 | if ( type->variable->assertions ) { |
---|
| 482 | type->variable->assertions->appendList( assertions ); |
---|
| 483 | } else { |
---|
| 484 | type->variable->assertions = assertions; |
---|
| 485 | } |
---|
| 486 | break; |
---|
| 487 | |
---|
| 488 | default: |
---|
| 489 | assert( false ); |
---|
[3848e0e] | 490 | } |
---|
[b87a5ed] | 491 | |
---|
| 492 | return this; |
---|
[51b7345] | 493 | } |
---|
| 494 | |
---|
[3848e0e] | 495 | DeclarationNode *DeclarationNode::addName( std::string* newname ) { |
---|
[b87a5ed] | 496 | name = assign_strptr( newname ); |
---|
| 497 | return this; |
---|
[51b7345] | 498 | } |
---|
| 499 | |
---|
[3848e0e] | 500 | DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) { |
---|
[b87a5ed] | 501 | bitfieldWidth = size; |
---|
| 502 | return this; |
---|
[51b7345] | 503 | } |
---|
| 504 | |
---|
[3848e0e] | 505 | DeclarationNode *DeclarationNode::addVarArgs() { |
---|
[b87a5ed] | 506 | assert( type ); |
---|
| 507 | hasEllipsis = true; |
---|
| 508 | return this; |
---|
[51b7345] | 509 | } |
---|
| 510 | |
---|
[3848e0e] | 511 | DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) { |
---|
[b87a5ed] | 512 | assert( type ); |
---|
| 513 | assert( type->kind == TypeData::Function ); |
---|
| 514 | assert( type->function->body == 0 ); |
---|
| 515 | type->function->body = body; |
---|
| 516 | type->function->hasBody = true; |
---|
| 517 | return this; |
---|
[51b7345] | 518 | } |
---|
| 519 | |
---|
[3848e0e] | 520 | DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) { |
---|
[b87a5ed] | 521 | assert( type ); |
---|
| 522 | assert( type->kind == TypeData::Function ); |
---|
| 523 | assert( type->function->oldDeclList == 0 ); |
---|
| 524 | type->function->oldDeclList = list; |
---|
| 525 | return this; |
---|
[51b7345] | 526 | } |
---|
| 527 | |
---|
| 528 | static void |
---|
[3848e0e] | 529 | setBase( TypeData *&type, TypeData *newType ) { |
---|
[b87a5ed] | 530 | if ( type ) { |
---|
| 531 | TypeData *prevBase = type; |
---|
| 532 | TypeData *curBase = type->base; |
---|
[a32b204] | 533 | while ( curBase != 0 ) { |
---|
[b87a5ed] | 534 | prevBase = curBase; |
---|
| 535 | curBase = curBase->base; |
---|
| 536 | } |
---|
| 537 | prevBase->base = newType; |
---|
| 538 | } else { |
---|
| 539 | type = newType; |
---|
[3848e0e] | 540 | } |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) { |
---|
[b87a5ed] | 544 | if ( p ) { |
---|
| 545 | assert( p->type->kind == TypeData::Pointer ); |
---|
| 546 | setBase( type, p->type ); |
---|
| 547 | p->type = 0; |
---|
| 548 | delete p; |
---|
| 549 | } |
---|
| 550 | return this; |
---|
[3848e0e] | 551 | } |
---|
| 552 | |
---|
| 553 | DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) { |
---|
[b87a5ed] | 554 | if ( a ) { |
---|
| 555 | assert( a->type->kind == TypeData::Array ); |
---|
| 556 | setBase( type, a->type ); |
---|
| 557 | a->type = 0; |
---|
| 558 | delete a; |
---|
| 559 | } |
---|
| 560 | return this; |
---|
[51b7345] | 561 | } |
---|
| 562 | |
---|
[3848e0e] | 563 | DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) { |
---|
[b87a5ed] | 564 | if ( p ) { |
---|
| 565 | assert( p->type->kind == TypeData::Pointer ); |
---|
| 566 | if ( type ) { |
---|
| 567 | switch ( type->kind ) { |
---|
| 568 | case TypeData::Aggregate: |
---|
| 569 | case TypeData::Enum: |
---|
| 570 | p->type->base = new TypeData( TypeData::AggregateInst ); |
---|
| 571 | p->type->base->aggInst->aggregate = type; |
---|
| 572 | if ( type->kind == TypeData::Aggregate ) { |
---|
| 573 | p->type->base->aggInst->params = maybeClone( type->aggregate->actuals ); |
---|
| 574 | } |
---|
| 575 | p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers ); |
---|
| 576 | break; |
---|
| 577 | |
---|
| 578 | default: |
---|
| 579 | p->type->base = type; |
---|
| 580 | } |
---|
| 581 | type = 0; |
---|
[3848e0e] | 582 | } |
---|
[b87a5ed] | 583 | delete this; |
---|
| 584 | return p; |
---|
| 585 | } else { |
---|
| 586 | return this; |
---|
| 587 | } |
---|
[51b7345] | 588 | } |
---|
| 589 | |
---|
[3848e0e] | 590 | static TypeData *findLast( TypeData *a ) { |
---|
[b87a5ed] | 591 | assert( a ); |
---|
| 592 | TypeData *cur = a; |
---|
[a32b204] | 593 | while ( cur->base ) { |
---|
[b87a5ed] | 594 | cur = cur->base; |
---|
| 595 | } |
---|
| 596 | return cur; |
---|
[3848e0e] | 597 | } |
---|
| 598 | |
---|
| 599 | DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) { |
---|
[b87a5ed] | 600 | if ( a ) { |
---|
| 601 | assert( a->type->kind == TypeData::Array ); |
---|
| 602 | TypeData *lastArray = findLast( a->type ); |
---|
| 603 | if ( type ) { |
---|
| 604 | switch ( type->kind ) { |
---|
| 605 | case TypeData::Aggregate: |
---|
| 606 | case TypeData::Enum: |
---|
| 607 | lastArray->base = new TypeData( TypeData::AggregateInst ); |
---|
| 608 | lastArray->base->aggInst->aggregate = type; |
---|
| 609 | if ( type->kind == TypeData::Aggregate ) { |
---|
| 610 | lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals ); |
---|
| 611 | } |
---|
| 612 | lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers ); |
---|
| 613 | break; |
---|
| 614 | default: |
---|
| 615 | lastArray->base = type; |
---|
| 616 | } |
---|
| 617 | type = 0; |
---|
[3848e0e] | 618 | } |
---|
[b87a5ed] | 619 | delete this; |
---|
| 620 | return a; |
---|
| 621 | } else { |
---|
| 622 | return this; |
---|
| 623 | } |
---|
[51b7345] | 624 | } |
---|
[3848e0e] | 625 | |
---|
| 626 | DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) { |
---|
[b87a5ed] | 627 | TypeData *ftype = new TypeData( TypeData::Function ); |
---|
| 628 | ftype->function->params = params; |
---|
| 629 | setBase( type, ftype ); |
---|
| 630 | return this; |
---|
[3848e0e] | 631 | } |
---|
| 632 | |
---|
| 633 | static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) { |
---|
[b87a5ed] | 634 | if ( type ) { |
---|
| 635 | if ( type->kind != TypeData::Function ) { |
---|
| 636 | type->base = addIdListToType( type->base, ids ); |
---|
| 637 | } else { |
---|
| 638 | type->function->idList = ids; |
---|
| 639 | } |
---|
| 640 | return type; |
---|
[3848e0e] | 641 | } else { |
---|
[b87a5ed] | 642 | TypeData *newtype = new TypeData( TypeData::Function ); |
---|
| 643 | newtype->function->idList = ids; |
---|
| 644 | return newtype; |
---|
[3848e0e] | 645 | } |
---|
| 646 | } |
---|
[b87a5ed] | 647 | |
---|
[3848e0e] | 648 | DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) { |
---|
[b87a5ed] | 649 | type = addIdListToType( type, ids ); |
---|
| 650 | return this; |
---|
[3848e0e] | 651 | } |
---|
| 652 | |
---|
| 653 | DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) { |
---|
[b87a5ed] | 654 | //assert |
---|
| 655 | initializer = init; |
---|
| 656 | return this; |
---|
[3848e0e] | 657 | } |
---|
| 658 | |
---|
| 659 | DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) { |
---|
[b87a5ed] | 660 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 661 | TypeData *srcType = type; |
---|
[a32b204] | 662 | while ( srcType->base ) { |
---|
[b87a5ed] | 663 | srcType = srcType->base; |
---|
[3848e0e] | 664 | } |
---|
[b87a5ed] | 665 | newnode->type = maybeClone( srcType ); |
---|
| 666 | if ( newnode->type->kind == TypeData::AggregateInst ) { |
---|
| 667 | // don't duplicate members |
---|
| 668 | if ( newnode->type->aggInst->aggregate->kind == TypeData::Enum ) { |
---|
| 669 | delete newnode->type->aggInst->aggregate->enumeration->constants; |
---|
| 670 | newnode->type->aggInst->aggregate->enumeration->constants = 0; |
---|
| 671 | } else { |
---|
| 672 | assert( newnode->type->aggInst->aggregate->kind == TypeData::Aggregate ); |
---|
| 673 | delete newnode->type->aggInst->aggregate->aggregate->members; |
---|
| 674 | newnode->type->aggInst->aggregate->aggregate->members = 0; |
---|
| 675 | } |
---|
| 676 | } |
---|
| 677 | newnode->type->forall = maybeClone( type->forall ); |
---|
| 678 | newnode->storageClasses = storageClasses; |
---|
| 679 | newnode->name = assign_strptr( newName ); |
---|
| 680 | return newnode; |
---|
[3848e0e] | 681 | } |
---|
| 682 | |
---|
| 683 | DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) { |
---|
[b87a5ed] | 684 | if ( o ) { |
---|
| 685 | o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() ); |
---|
| 686 | if ( type ) { |
---|
| 687 | TypeData *srcType = type; |
---|
[a32b204] | 688 | while ( srcType->base ) { |
---|
[b87a5ed] | 689 | srcType = srcType->base; |
---|
| 690 | } |
---|
| 691 | TypeData *newType = srcType->clone(); |
---|
| 692 | if ( newType->kind == TypeData::AggregateInst ) { |
---|
| 693 | // don't duplicate members |
---|
| 694 | if ( newType->aggInst->aggregate->kind == TypeData::Enum ) { |
---|
| 695 | delete newType->aggInst->aggregate->enumeration->constants; |
---|
| 696 | newType->aggInst->aggregate->enumeration->constants = 0; |
---|
| 697 | } else { |
---|
| 698 | assert( newType->aggInst->aggregate->kind == TypeData::Aggregate ); |
---|
| 699 | delete newType->aggInst->aggregate->aggregate->members; |
---|
| 700 | newType->aggInst->aggregate->aggregate->members = 0; |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | newType->forall = maybeClone( type->forall ); |
---|
| 704 | if ( ! o->type ) { |
---|
| 705 | o->type = newType; |
---|
| 706 | } else { |
---|
| 707 | addTypeToType( newType, o->type ); |
---|
| 708 | delete newType; |
---|
| 709 | } |
---|
[3848e0e] | 710 | } |
---|
| 711 | } |
---|
[b87a5ed] | 712 | return o; |
---|
[3848e0e] | 713 | } |
---|
| 714 | |
---|
| 715 | DeclarationNode *DeclarationNode::cloneType( string *newName ) { |
---|
[b87a5ed] | 716 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 717 | newnode->type = maybeClone( type ); |
---|
| 718 | newnode->storageClasses = storageClasses; |
---|
| 719 | newnode->name = assign_strptr( newName ); |
---|
| 720 | return newnode; |
---|
[3848e0e] | 721 | } |
---|
| 722 | |
---|
| 723 | DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) { |
---|
[b87a5ed] | 724 | if ( o ) { |
---|
| 725 | o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() ); |
---|
| 726 | if ( type ) { |
---|
| 727 | TypeData *newType = type->clone(); |
---|
| 728 | if ( ! o->type ) { |
---|
| 729 | o->type = newType; |
---|
| 730 | } else { |
---|
| 731 | addTypeToType( newType, o->type ); |
---|
| 732 | delete newType; |
---|
| 733 | } |
---|
| 734 | } |
---|
[3848e0e] | 735 | } |
---|
[b87a5ed] | 736 | return o; |
---|
[51b7345] | 737 | } |
---|
| 738 | |
---|
[3848e0e] | 739 | DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) { |
---|
[b87a5ed] | 740 | if ( node != 0 ) { |
---|
| 741 | set_link( node ); |
---|
| 742 | } |
---|
| 743 | return this; |
---|
[51b7345] | 744 | } |
---|
| 745 | |
---|
[3848e0e] | 746 | DeclarationNode *DeclarationNode::extractAggregate() const { |
---|
[b87a5ed] | 747 | if ( type ) { |
---|
| 748 | TypeData *ret = type->extractAggregate(); |
---|
| 749 | if ( ret ) { |
---|
| 750 | DeclarationNode *newnode = new DeclarationNode; |
---|
| 751 | newnode->type = ret; |
---|
| 752 | return newnode; |
---|
[843054c2] | 753 | } // if |
---|
| 754 | } // if |
---|
| 755 | return 0; |
---|
[3848e0e] | 756 | } |
---|
| 757 | |
---|
| 758 | void buildList( const DeclarationNode *firstNode, std::list< Declaration* > &outputList ) { |
---|
[b87a5ed] | 759 | SemanticError errors; |
---|
| 760 | std::back_insert_iterator< std::list< Declaration* > > out( outputList ); |
---|
| 761 | const DeclarationNode *cur = firstNode; |
---|
[a32b204] | 762 | while ( cur ) { |
---|
[b87a5ed] | 763 | try { |
---|
| 764 | if ( DeclarationNode *extr = cur->extractAggregate() ) { |
---|
| 765 | // handle the case where a structure declaration is contained within an object or type declaration |
---|
| 766 | Declaration *decl = extr->build(); |
---|
| 767 | if ( decl ) { |
---|
| 768 | *out++ = decl; |
---|
[843054c2] | 769 | } // if |
---|
| 770 | } // if |
---|
[b87a5ed] | 771 | Declaration *decl = cur->build(); |
---|
| 772 | if ( decl ) { |
---|
| 773 | *out++ = decl; |
---|
[843054c2] | 774 | } // if |
---|
[b87a5ed] | 775 | } catch( SemanticError &e ) { |
---|
| 776 | errors.append( e ); |
---|
[843054c2] | 777 | } // try |
---|
[b87a5ed] | 778 | cur = dynamic_cast< DeclarationNode* >( cur->get_link() ); |
---|
[843054c2] | 779 | } // while |
---|
[b87a5ed] | 780 | if ( ! errors.isEmpty() ) { |
---|
| 781 | throw errors; |
---|
[843054c2] | 782 | } // if |
---|
[3848e0e] | 783 | } |
---|
| 784 | |
---|
| 785 | void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType* > &outputList ) { |
---|
[b87a5ed] | 786 | SemanticError errors; |
---|
| 787 | std::back_insert_iterator< std::list< DeclarationWithType* > > out( outputList ); |
---|
| 788 | const DeclarationNode *cur = firstNode; |
---|
[a32b204] | 789 | while ( cur ) { |
---|
[b87a5ed] | 790 | try { |
---|
[3848e0e] | 791 | /// if ( DeclarationNode *extr = cur->extractAggregate() ) { |
---|
[51b7345] | 792 | /// // handle the case where a structure declaration is contained within an object or type |
---|
| 793 | /// // declaration |
---|
| 794 | /// Declaration *decl = extr->build(); |
---|
[3848e0e] | 795 | /// if ( decl ) { |
---|
| 796 | /// *out++ = decl; |
---|
[51b7345] | 797 | /// } |
---|
| 798 | /// } |
---|
[b87a5ed] | 799 | Declaration *decl = cur->build(); |
---|
| 800 | if ( decl ) { |
---|
| 801 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) { |
---|
| 802 | *out++ = dwt; |
---|
| 803 | } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) { |
---|
| 804 | StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() ); |
---|
| 805 | *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); |
---|
| 806 | delete agg; |
---|
| 807 | } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) { |
---|
| 808 | UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() ); |
---|
| 809 | *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 ); |
---|
[843054c2] | 810 | } // if |
---|
| 811 | } // if |
---|
[b87a5ed] | 812 | } catch( SemanticError &e ) { |
---|
| 813 | errors.append( e ); |
---|
[843054c2] | 814 | } // try |
---|
[b87a5ed] | 815 | cur = dynamic_cast< DeclarationNode* >( cur->get_link() ); |
---|
[843054c2] | 816 | } // while |
---|
[b87a5ed] | 817 | if ( ! errors.isEmpty() ) { |
---|
| 818 | throw errors; |
---|
[843054c2] | 819 | } // if |
---|
[3848e0e] | 820 | } |
---|
| 821 | |
---|
| 822 | void buildTypeList( const DeclarationNode *firstNode, std::list< Type* > &outputList ) { |
---|
[b87a5ed] | 823 | SemanticError errors; |
---|
| 824 | std::back_insert_iterator< std::list< Type* > > out( outputList ); |
---|
| 825 | const DeclarationNode *cur = firstNode; |
---|
[a32b204] | 826 | while ( cur ) { |
---|
[b87a5ed] | 827 | try { |
---|
| 828 | *out++ = cur->buildType(); |
---|
| 829 | } catch( SemanticError &e ) { |
---|
| 830 | errors.append( e ); |
---|
[843054c2] | 831 | } // try |
---|
[b87a5ed] | 832 | cur = dynamic_cast< DeclarationNode* >( cur->get_link() ); |
---|
[843054c2] | 833 | } // while |
---|
[b87a5ed] | 834 | if ( ! errors.isEmpty() ) { |
---|
| 835 | throw errors; |
---|
[843054c2] | 836 | } // if |
---|
[51b7345] | 837 | } |
---|
| 838 | |
---|
[3848e0e] | 839 | Declaration *DeclarationNode::build() const { |
---|
[843054c2] | 840 | if ( type ) { |
---|
[b87a5ed] | 841 | Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildInline(), linkage, maybeBuild< Initializer >(initializer) ); |
---|
| 842 | return newDecl; |
---|
[843054c2] | 843 | } // if |
---|
| 844 | if ( ! buildInline() ) { |
---|
| 845 | return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ); |
---|
| 846 | } // if |
---|
| 847 | throw SemanticError( "invalid inline specification in declaration of ", this ); |
---|
[51b7345] | 848 | } |
---|
| 849 | |
---|
[3848e0e] | 850 | Type *DeclarationNode::buildType() const { |
---|
[b87a5ed] | 851 | assert( type ); |
---|
[51b7345] | 852 | |
---|
[b87a5ed] | 853 | switch ( type->kind ) { |
---|
| 854 | case TypeData::Enum: |
---|
| 855 | return new EnumInstType( type->buildQualifiers(), type->enumeration->name ); |
---|
| 856 | case TypeData::Aggregate: { |
---|
| 857 | ReferenceToType *ret; |
---|
| 858 | switch ( type->aggregate->kind ) { |
---|
| 859 | case DeclarationNode::Struct: |
---|
| 860 | ret = new StructInstType( type->buildQualifiers(), type->aggregate->name ); |
---|
| 861 | break; |
---|
| 862 | case DeclarationNode::Union: |
---|
| 863 | ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name ); |
---|
| 864 | break; |
---|
| 865 | case DeclarationNode::Context: |
---|
| 866 | ret = new ContextInstType( type->buildQualifiers(), type->aggregate->name ); |
---|
| 867 | break; |
---|
| 868 | default: |
---|
| 869 | assert( false ); |
---|
| 870 | } // switch |
---|
| 871 | buildList( type->aggregate->actuals, ret->get_parameters() ); |
---|
| 872 | return ret; |
---|
| 873 | } |
---|
| 874 | case TypeData::Symbolic: { |
---|
| 875 | TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false ); |
---|
| 876 | buildList( type->symbolic->actuals, ret->get_parameters() ); |
---|
| 877 | return ret; |
---|
| 878 | } |
---|
| 879 | default: |
---|
| 880 | return type->build(); |
---|
| 881 | } // switch |
---|
[3848e0e] | 882 | } |
---|
| 883 | |
---|
| 884 | Declaration::StorageClass DeclarationNode::buildStorageClass() const { |
---|
[b87a5ed] | 885 | static const Declaration::StorageClass scMap[] = { |
---|
| 886 | Declaration::Extern, |
---|
| 887 | Declaration::Static, |
---|
| 888 | Declaration::Auto, |
---|
| 889 | Declaration::Register, |
---|
| 890 | Declaration::Inline, |
---|
| 891 | Declaration::Fortran |
---|
| 892 | }; |
---|
[51b7345] | 893 | |
---|
[b87a5ed] | 894 | Declaration::StorageClass ret = Declaration::NoStorageClass; |
---|
| 895 | for ( std::list< StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) { |
---|
| 896 | assert( unsigned( *i ) < sizeof( scMap ) / sizeof( scMap[0] ) ); |
---|
[843054c2] | 897 | if ( *i == Inline ) continue; |
---|
| 898 | if ( ret != Declaration::NoStorageClass ) { |
---|
[b87a5ed] | 899 | throw SemanticError( "invalid combination of storage classes in declaration of ", this ); |
---|
| 900 | } |
---|
[843054c2] | 901 | ret = scMap[ *i ]; |
---|
[3848e0e] | 902 | } |
---|
[b87a5ed] | 903 | return ret; |
---|
[51b7345] | 904 | } |
---|
| 905 | |
---|
[3848e0e] | 906 | bool DeclarationNode::buildInline() const { |
---|
[b87a5ed] | 907 | std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline ); |
---|
[843054c2] | 908 | if ( first == storageClasses.end() ) return false; |
---|
| 909 | std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline ); |
---|
| 910 | if ( next == storageClasses.end() ) return true; |
---|
| 911 | throw SemanticError( "duplicate inline specification in declaration of ", this ); |
---|
[51b7345] | 912 | } |
---|
[b87a5ed] | 913 | |
---|
| 914 | // Local Variables: // |
---|
| 915 | // tab-width: 4 // |
---|
| 916 | // mode: c++ // |
---|
| 917 | // compile-command: "make install" // |
---|
| 918 | // End: // |
---|