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