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