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