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