source: src/Parser/DeclarationNode.cc@ 704d11e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 704d11e was 704d11e, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Minor cleanup

  • Property mode set to 100644
File size: 37.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// DeclarationNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jun 7 12:08:55 2018
13// Update Count : 1079
14//
15
16#include <cassert> // for assert, assertf, strict_dynamic_cast
17#include <iterator> // for back_insert_iterator
18#include <list> // for list
19#include <memory> // for unique_ptr
20#include <ostream> // for operator<<, ostream, basic_ostream
21#include <string> // for string, operator+, allocator, char...
22
23#include "Common/SemanticError.h" // for SemanticError
24#include "Common/UniqueName.h" // for UniqueName
25#include "Common/utility.h" // for maybeClone, maybeBuild, CodeLocation
26#include "Parser/LinkageSpec.h" // for Spec, linkageName, Cforall
27#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
28#include "SynTree/Attribute.h" // for Attribute
29#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, Declaration
30#include "SynTree/Expression.h" // for Expression, ConstantExpr
31#include "SynTree/Statement.h" // for AsmStmt
32#include "SynTree/Type.h" // for Type, Type::StorageClasses, Type::...
33#include "TypeData.h" // for TypeData, TypeData::Aggregate_t
34#include "TypedefTable.h" // for TypedefTable
35
36class Initializer;
37
38extern TypedefTable typedefTable;
39
40using namespace std;
41
42// These must harmonize with the corresponding DeclarationNode enumerations.
43const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
44const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
45const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
46const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
47const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
48const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
49const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" };
50
51UniqueName DeclarationNode::anonymous( "__anonymous" );
52
53extern LinkageSpec::Spec linkage; // defined in parser.yy
54
55DeclarationNode::DeclarationNode() :
56 builtin( NoBuiltinType ),
57 type( nullptr ),
58 bitfieldWidth( nullptr ),
59 hasEllipsis( false ),
60 linkage( ::linkage ),
61 asmName( nullptr ),
62 initializer( nullptr ),
63 extension( false ),
64 asmStmt( nullptr ) {
65
66// variable.name = nullptr;
67 variable.tyClass = NoTypeClass;
68 variable.assertions = nullptr;
69 variable.initializer = nullptr;
70
71// attr.name = nullptr;
72 attr.expr = nullptr;
73 attr.type = nullptr;
74
75 assert.condition = nullptr;
76 assert.message = nullptr;
77}
78
79DeclarationNode::~DeclarationNode() {
80// delete attr.name;
81 delete attr.expr;
82 delete attr.type;
83
84// delete variable.name;
85 delete variable.assertions;
86 delete variable.initializer;
87
88 delete type;
89 delete bitfieldWidth;
90
91 delete asmStmt;
92 // asmName, no delete, passed to next stage
93 delete initializer;
94
95 delete assert.condition;
96 delete assert.message;
97}
98
99DeclarationNode * DeclarationNode::clone() const {
100 DeclarationNode * newnode = new DeclarationNode;
101 newnode->set_next( maybeClone( get_next() ) );
102 newnode->name = name ? new string( *name ) : nullptr;
103
104 newnode->builtin = NoBuiltinType;
105 newnode->type = maybeClone( type );
106 newnode->storageClasses = storageClasses;
107 newnode->funcSpecs = funcSpecs;
108 newnode->bitfieldWidth = maybeClone( bitfieldWidth );
109 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
110 newnode->hasEllipsis = hasEllipsis;
111 newnode->linkage = linkage;
112 newnode->asmName = maybeClone( asmName );
113 cloneAll( attributes, newnode->attributes );
114 newnode->initializer = maybeClone( initializer );
115 newnode->extension = extension;
116 newnode->asmStmt = maybeClone( asmStmt );
117 newnode->error = error;
118
119// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
120 newnode->variable.tyClass = variable.tyClass;
121 newnode->variable.assertions = maybeClone( variable.assertions );
122 newnode->variable.initializer = maybeClone( variable.initializer );
123
124// newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
125 newnode->attr.expr = maybeClone( attr.expr );
126 newnode->attr.type = maybeClone( attr.type );
127
128 newnode->assert.condition = maybeClone( assert.condition );
129 newnode->assert.message = maybeClone( assert.message );
130 return newnode;
131} // DeclarationNode::clone
132
133void DeclarationNode::print( std::ostream &os, int indent ) const {
134 os << string( indent, ' ' );
135 if ( name ) {
136 os << *name << ": ";
137 } else {
138 os << "unnamed: ";
139 } // if
140
141 if ( linkage != LinkageSpec::Cforall ) {
142 os << LinkageSpec::linkageName( linkage ) << " ";
143 } // if
144
145 storageClasses.print( os );
146 funcSpecs.print( os );
147
148 if ( type ) {
149 type->print( os, indent );
150 } else {
151 os << "untyped entity ";
152 } // if
153
154 if ( bitfieldWidth ) {
155 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
156 bitfieldWidth->printOneLine( os );
157 } // if
158
159 if ( initializer ) {
160 os << endl << string( indent + 2, ' ' ) << "with initializer ";
161 initializer->printOneLine( os );
162 os << " maybe constructed? " << initializer->get_maybeConstructed();
163
164 } // if
165
166 os << endl;
167}
168
169void DeclarationNode::printList( std::ostream &os, int indent ) const {
170 ParseNode::printList( os, indent );
171 if ( hasEllipsis ) {
172 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
173 } // if
174}
175
176DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
177 DeclarationNode * newnode = new DeclarationNode;
178 newnode->name = name;
179 newnode->type = new TypeData( TypeData::Function );
180 newnode->type->function.params = param;
181 newnode->type->function.body = body;
182
183 if ( ret ) {
184 newnode->type->base = ret->type;
185 ret->type = nullptr;
186 delete ret;
187 } // if
188
189 return newnode;
190} // DeclarationNode::newFunction
191
192
193DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
194 DeclarationNode * newnode = new DeclarationNode;
195 newnode->storageClasses = sc;
196 return newnode;
197} // DeclarationNode::newStorageClass
198
199DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
200 DeclarationNode * newnode = new DeclarationNode;
201 newnode->funcSpecs = fs;
202 return newnode;
203} // DeclarationNode::newFuncSpecifier
204
205DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
206 DeclarationNode * newnode = new DeclarationNode;
207 newnode->type = new TypeData();
208 newnode->type->qualifiers = tq;
209 return newnode;
210} // DeclarationNode::newQualifier
211
212DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
213 DeclarationNode * newnode = new DeclarationNode;
214 newnode->type = new TypeData( TypeData::Basic );
215 newnode->type->basictype = bt;
216 return newnode;
217} // DeclarationNode::newBasicType
218
219DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
220 DeclarationNode * newnode = new DeclarationNode;
221 newnode->type = new TypeData( TypeData::Basic );
222 newnode->type->complextype = ct;
223 return newnode;
224} // DeclarationNode::newComplexType
225
226DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
227 DeclarationNode * newnode = new DeclarationNode;
228 newnode->type = new TypeData( TypeData::Basic );
229 newnode->type->signedness = sn;
230 return newnode;
231} // DeclarationNode::newSignedNess
232
233DeclarationNode * DeclarationNode::newLength( Length lnth ) {
234 DeclarationNode * newnode = new DeclarationNode;
235 newnode->type = new TypeData( TypeData::Basic );
236 newnode->type->length = lnth;
237 return newnode;
238} // DeclarationNode::newLength
239
240DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
241 DeclarationNode * newnode = new DeclarationNode;
242 newnode->type = new TypeData( TypeData::Unknown );
243 newnode->type->forall = forall;
244 return newnode;
245} // DeclarationNode::newForall
246
247DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
248 DeclarationNode * newnode = new DeclarationNode;
249 newnode->type = new TypeData( TypeData::SymbolicInst );
250 newnode->type->symbolic.name = name;
251 newnode->type->symbolic.isTypedef = true;
252 newnode->type->symbolic.params = nullptr;
253 return newnode;
254} // DeclarationNode::newFromTypedef
255
256DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
257 return child;
258 // return parent->add_last( child );
259}
260
261DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
262 assert( name );
263 DeclarationNode * newnode = new DeclarationNode;
264 newnode->type = new TypeData( TypeData::Aggregate );
265 newnode->type->aggregate.kind = kind;
266 newnode->type->aggregate.name = name;
267 newnode->type->aggregate.actuals = actuals;
268 newnode->type->aggregate.fields = fields;
269 newnode->type->aggregate.body = body;
270 newnode->type->aggregate.tagged = false;
271 newnode->type->aggregate.parent = nullptr;
272 return newnode;
273} // DeclarationNode::newAggregate
274
275DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
276 assert( name );
277 DeclarationNode * newnode = new DeclarationNode;
278 newnode->type = new TypeData( TypeData::Enum );
279 newnode->type->enumeration.name = name;
280 newnode->type->enumeration.constants = constants;
281 newnode->type->enumeration.body = body;
282 return newnode;
283} // DeclarationNode::newEnum
284
285DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
286 DeclarationNode * newnode = new DeclarationNode;
287 newnode->name = name;
288 newnode->enumeratorValue.reset( constant );
289 return newnode;
290} // DeclarationNode::newEnumConstant
291
292DeclarationNode * DeclarationNode::newName( const string * name ) {
293 DeclarationNode * newnode = new DeclarationNode;
294 newnode->name = name;
295 return newnode;
296} // DeclarationNode::newName
297
298DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
299 DeclarationNode * newnode = new DeclarationNode;
300 newnode->type = new TypeData( TypeData::SymbolicInst );
301 newnode->type->symbolic.name = name;
302 newnode->type->symbolic.isTypedef = false;
303 newnode->type->symbolic.actuals = params;
304 return newnode;
305} // DeclarationNode::newFromTypeGen
306
307DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, const string * name ) {
308 DeclarationNode * newnode = new DeclarationNode;
309 newnode->type = nullptr;
310 assert( ! newnode->name );
311// newnode->variable.name = name;
312 newnode->name = name;
313 newnode->variable.tyClass = tc;
314 newnode->variable.assertions = nullptr;
315 return newnode;
316} // DeclarationNode::newTypeParam
317
318DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
319 DeclarationNode * newnode = new DeclarationNode;
320 newnode->type = new TypeData( TypeData::Aggregate );
321 newnode->type->aggregate.name = name;
322 newnode->type->aggregate.kind = Trait;
323 newnode->type->aggregate.params = params;
324 newnode->type->aggregate.fields = asserts;
325 return newnode;
326} // DeclarationNode::newTrait
327
328DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
329 DeclarationNode * newnode = new DeclarationNode;
330 newnode->type = new TypeData( TypeData::AggregateInst );
331 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
332 newnode->type->aggInst.aggregate->aggregate.kind = Trait;
333 newnode->type->aggInst.aggregate->aggregate.name = name;
334 newnode->type->aggInst.params = params;
335 return newnode;
336} // DeclarationNode::newTraitUse
337
338DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
339 DeclarationNode * newnode = new DeclarationNode;
340 newnode->name = name;
341 newnode->type = new TypeData( TypeData::Symbolic );
342 newnode->type->symbolic.isTypedef = false;
343 newnode->type->symbolic.params = typeParams;
344 return newnode;
345} // DeclarationNode::newTypeDecl
346
347DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
348 DeclarationNode * newnode = new DeclarationNode;
349 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
350 if ( kind == OperKinds::And ) {
351 // T && is parsed as 'And' operator rather than two references => add a second reference type
352 TypeData * td = new TypeData( TypeData::Reference );
353 td->base = newnode->type;
354 newnode->type = td;
355 }
356 if ( qualifiers ) {
357 return newnode->addQualifiers( qualifiers );
358 } else {
359 return newnode;
360 } // if
361} // DeclarationNode::newPointer
362
363DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
364 DeclarationNode * newnode = new DeclarationNode;
365 newnode->type = new TypeData( TypeData::Array );
366 newnode->type->array.dimension = size;
367 newnode->type->array.isStatic = isStatic;
368 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
369 newnode->type->array.isVarLen = false;
370 } else {
371 newnode->type->array.isVarLen = true;
372 } // if
373 return newnode->addQualifiers( qualifiers );
374} // DeclarationNode::newArray
375
376DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
377 DeclarationNode * newnode = new DeclarationNode;
378 newnode->type = new TypeData( TypeData::Array );
379 newnode->type->array.dimension = nullptr;
380 newnode->type->array.isStatic = false;
381 newnode->type->array.isVarLen = true;
382 return newnode->addQualifiers( qualifiers );
383}
384
385DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
386 DeclarationNode * newnode = new DeclarationNode;
387 newnode->bitfieldWidth = size;
388 return newnode;
389}
390
391DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
392 DeclarationNode * newnode = new DeclarationNode;
393 newnode->type = new TypeData( TypeData::Tuple );
394 newnode->type->tuple = members;
395 return newnode;
396}
397
398DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
399 DeclarationNode * newnode = new DeclarationNode;
400 newnode->type = new TypeData( TypeData::Typeof );
401 newnode->type->typeexpr = expr;
402 return newnode;
403}
404
405DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
406 DeclarationNode * newnode = new DeclarationNode;
407 newnode->type = new TypeData( TypeData::Builtin );
408 newnode->builtin = bt;
409 newnode->type->builtintype = newnode->builtin;
410 return newnode;
411} // DeclarationNode::newBuiltinType
412
413DeclarationNode * DeclarationNode::newAttr( const string * name, ExpressionNode * expr ) {
414 DeclarationNode * newnode = new DeclarationNode;
415 newnode->type = nullptr;
416// newnode->attr.name = name;
417 newnode->name = name;
418 newnode->attr.expr = expr;
419 return newnode;
420}
421
422DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {
423 DeclarationNode * newnode = new DeclarationNode;
424 newnode->type = nullptr;
425// newnode->attr.name = name;
426 newnode->name = name;
427 newnode->attr.type = type;
428 return newnode;
429}
430
431DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
432 DeclarationNode * newnode = new DeclarationNode;
433 newnode->type = nullptr;
434 std::list< Expression * > exprs;
435 buildList( expr, exprs );
436 newnode->attributes.push_back( new Attribute( *name, exprs ) );
437 delete name;
438 return newnode;
439}
440
441DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
442 DeclarationNode * newnode = new DeclarationNode;
443 newnode->asmStmt = stmt;
444 return newnode;
445}
446
447DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) {
448 DeclarationNode * newnode = new DeclarationNode;
449 newnode->assert.condition = condition;
450 newnode->assert.message = message;
451 return newnode;
452}
453
454
455void appendError( string & dst, const string & src ) {
456 if ( src.empty() ) return;
457 if ( dst.empty() ) { dst = src; return; }
458 dst += ", " + src;
459} // appendError
460
461void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
462 const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
463
464 if ( (qsrc & qdst).any() ) { // duplicates ?
465 for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
466 if ( qsrc[i] && qdst[i] ) {
467 appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
468 } // if
469 } // for
470 } // for
471} // DeclarationNode::checkQualifiers
472
473void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
474 if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ?
475 for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
476 if ( funcSpecs[i] && src->funcSpecs[i] ) {
477 appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
478 } // if
479 } // for
480 } // if
481
482 if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
483 if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
484 for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
485 if ( storageClasses[i] && src->storageClasses[i] ) {
486 appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
487 } // if
488 } // for
489 // src is the new item being added and has a single bit
490 } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
491 appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
492 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
493 src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier
494 } // if
495 } // if
496
497 appendError( error, src->error );
498} // DeclarationNode::checkSpecifiers
499
500DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
501 funcSpecs |= q->funcSpecs;
502 storageClasses |= q->storageClasses;
503
504 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
505 attributes.push_front( attr->clone() );
506 } // for
507 return this;
508} // DeclarationNode::copySpecifiers
509
510static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
511 if ( src->forall && dst->kind == TypeData::Function ) {
512 if ( dst->forall ) {
513 dst->forall->appendList( src->forall );
514 } else {
515 dst->forall = src->forall;
516 } // if
517 src->forall = nullptr;
518 } // if
519 if ( dst->base ) {
520 addQualifiersToType( src, dst->base );
521 } else if ( dst->kind == TypeData::Function ) {
522 dst->base = src;
523 src = nullptr;
524 } else {
525 dst->qualifiers |= src->qualifiers;
526 } // if
527} // addQualifiersToType
528
529DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
530 if ( ! q ) { return this; } // empty qualifier
531
532 checkSpecifiers( q );
533 copySpecifiers( q );
534
535 if ( ! q->type ) { delete q; return this; }
536
537 if ( ! type ) {
538 type = q->type; // reuse structure
539 q->type = nullptr;
540 delete q;
541 return this;
542 } // if
543
544 if ( q->type->forall ) { // forall qualifier ?
545 if ( type->forall ) { // polymorphic routine ?
546 type->forall->appendList( q->type->forall ); // augment forall qualifier
547 } else {
548 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
549 if ( type->aggregate.params ) { // polymorphic ?
550 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
551 } else { // not polymorphic
552 type->aggregate.params = q->type->forall; // set forall qualifier
553 } // if
554 } else { // not polymorphic
555 type->forall = q->type->forall; // make polymorphic routine
556 } // if
557 } // if
558 q->type->forall = nullptr; // forall qualifier moved
559 } // if
560
561 checkQualifiers( type, q->type );
562 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
563 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
564 } // if
565 addQualifiersToType( q->type, type );
566
567 delete q;
568 return this;
569} // addQualifiers
570
571static void addTypeToType( TypeData *&src, TypeData *&dst ) {
572 if ( src->forall && dst->kind == TypeData::Function ) {
573 if ( dst->forall ) {
574 dst->forall->appendList( src->forall );
575 } else {
576 dst->forall = src->forall;
577 } // if
578 src->forall = nullptr;
579 } // if
580 if ( dst->base ) {
581 addTypeToType( src, dst->base );
582 } else {
583 switch ( dst->kind ) {
584 case TypeData::Unknown:
585 src->qualifiers |= dst->qualifiers;
586 dst = src;
587 src = nullptr;
588 break;
589 case TypeData::Basic:
590 dst->qualifiers |= src->qualifiers;
591 if ( src->kind != TypeData::Unknown ) {
592 assert( src->kind == TypeData::Basic );
593
594 if ( dst->basictype == DeclarationNode::NoBasicType ) {
595 dst->basictype = src->basictype;
596 } else if ( src->basictype != DeclarationNode::NoBasicType )
597 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
598
599 if ( dst->complextype == DeclarationNode::NoComplexType ) {
600 dst->complextype = src->complextype;
601 } else if ( src->complextype != DeclarationNode::NoComplexType )
602 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
603
604 if ( dst->signedness == DeclarationNode::NoSignedness ) {
605 dst->signedness = src->signedness;
606 } else if ( src->signedness != DeclarationNode::NoSignedness )
607 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
608
609 if ( dst->length == DeclarationNode::NoLength ) {
610 dst->length = src->length;
611 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
612 dst->length = DeclarationNode::LongLong;
613 } else if ( src->length != DeclarationNode::NoLength )
614 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
615 } // if
616 break;
617 default:
618 switch ( src->kind ) {
619 case TypeData::Aggregate:
620 case TypeData::Enum:
621 dst->base = new TypeData( TypeData::AggregateInst );
622 dst->base->aggInst.aggregate = src;
623 if ( src->kind == TypeData::Aggregate ) {
624 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
625 } // if
626 dst->base->qualifiers |= src->qualifiers;
627 src = nullptr;
628 break;
629 default:
630 if ( dst->forall ) {
631 dst->forall->appendList( src->forall );
632 } else {
633 dst->forall = src->forall;
634 } // if
635 src->forall = nullptr;
636 dst->base = src;
637 src = nullptr;
638 } // switch
639 } // switch
640 } // if
641}
642
643DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
644 if ( o ) {
645 checkSpecifiers( o );
646 copySpecifiers( o );
647 if ( o->type ) {
648 if ( ! type ) {
649 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
650 type = new TypeData( TypeData::AggregateInst );
651 type->aggInst.aggregate = o->type;
652 if ( o->type->kind == TypeData::Aggregate ) {
653 type->aggInst.hoistType = o->type->aggregate.body;
654 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
655 } else {
656 type->aggInst.hoistType = o->type->enumeration.body;
657 } // if
658 type->qualifiers |= o->type->qualifiers;
659 } else {
660 type = o->type;
661 } // if
662 o->type = nullptr;
663 } else {
664 addTypeToType( o->type, type );
665 } // if
666 } // if
667 if ( o->bitfieldWidth ) {
668 bitfieldWidth = o->bitfieldWidth;
669 } // if
670
671 // there may be typedefs chained onto the type
672 if ( o->get_next() ) {
673 set_last( o->get_next()->clone() );
674 } // if
675 } // if
676 delete o;
677 return this;
678}
679
680DeclarationNode * DeclarationNode::addTypedef() {
681 TypeData * newtype = new TypeData( TypeData::Symbolic );
682 newtype->symbolic.params = nullptr;
683 newtype->symbolic.isTypedef = true;
684 newtype->symbolic.name = name ? new string( *name ) : nullptr;
685 newtype->base = type;
686 type = newtype;
687 return this;
688}
689
690DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
691 if ( variable.tyClass != NoTypeClass ) {
692 if ( variable.assertions ) {
693 variable.assertions->appendList( assertions );
694 } else {
695 variable.assertions = assertions;
696 } // if
697 return this;
698 } // if
699
700 assert( type );
701 switch ( type->kind ) {
702 case TypeData::Symbolic:
703 if ( type->symbolic.assertions ) {
704 type->symbolic.assertions->appendList( assertions );
705 } else {
706 type->symbolic.assertions = assertions;
707 } // if
708 break;
709 default:
710 assert( false );
711 } // switch
712
713 return this;
714}
715
716DeclarationNode * DeclarationNode::addName( string * newname ) {
717 assert( ! name );
718 name = newname;
719 return this;
720}
721
722DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
723 assert( ! asmName );
724 asmName = newname ? newname->asmName : nullptr;
725 return this->addQualifiers( newname );
726}
727
728DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
729 bitfieldWidth = size;
730 return this;
731}
732
733DeclarationNode * DeclarationNode::addVarArgs() {
734 assert( type );
735 hasEllipsis = true;
736 return this;
737}
738
739DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
740 assert( type );
741 assert( type->kind == TypeData::Function );
742 assert( ! type->function.body );
743 type->function.body = body;
744 type->function.withExprs = withExprs;
745 return this;
746}
747
748DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
749 assert( type );
750 assert( type->kind == TypeData::Function );
751 assert( ! type->function.oldDeclList );
752 type->function.oldDeclList = list;
753 return this;
754}
755
756DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
757 if ( type ) {
758 TypeData * prevBase = type;
759 TypeData * curBase = type->base;
760 while ( curBase != nullptr ) {
761 prevBase = curBase;
762 curBase = curBase->base;
763 } // while
764 prevBase->base = newType;
765 } else {
766 type = newType;
767 } // if
768 return this;
769}
770
771DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
772 if ( a ) {
773 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
774 attributes.push_front( attr );
775 } // for
776 a->attributes.clear();
777 } // if
778 return this;
779} // copyAttribute
780
781DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
782 if ( p ) {
783 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
784 setBase( p->type );
785 p->type = nullptr;
786 copyAttribute( p );
787 delete p;
788 } // if
789 return this;
790}
791
792DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
793 if ( a ) {
794 assert( a->type->kind == TypeData::Array );
795 setBase( a->type );
796 a->type = nullptr;
797 copyAttribute( a );
798 delete a;
799 } // if
800 return this;
801}
802
803DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
804 if ( p ) {
805 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
806 if ( type ) {
807 switch ( type->kind ) {
808 case TypeData::Aggregate:
809 case TypeData::Enum:
810 p->type->base = new TypeData( TypeData::AggregateInst );
811 p->type->base->aggInst.aggregate = type;
812 if ( type->kind == TypeData::Aggregate ) {
813 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
814 } // if
815 p->type->base->qualifiers |= type->qualifiers;
816 break;
817
818 default:
819 p->type->base = type;
820 } // switch
821 type = nullptr;
822 } // if
823 delete this;
824 return p;
825 } else {
826 return this;
827 } // if
828}
829
830static TypeData * findLast( TypeData * a ) {
831 assert( a );
832 TypeData * cur = a;
833 while ( cur->base ) {
834 cur = cur->base;
835 } // while
836 return cur;
837}
838
839DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
840 if ( ! a ) return this;
841 assert( a->type->kind == TypeData::Array );
842 TypeData * lastArray = findLast( a->type );
843 if ( type ) {
844 switch ( type->kind ) {
845 case TypeData::Aggregate:
846 case TypeData::Enum:
847 lastArray->base = new TypeData( TypeData::AggregateInst );
848 lastArray->base->aggInst.aggregate = type;
849 if ( type->kind == TypeData::Aggregate ) {
850 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
851 } // if
852 lastArray->base->qualifiers |= type->qualifiers;
853 break;
854 default:
855 lastArray->base = type;
856 } // switch
857 type = nullptr;
858 } // if
859 delete this;
860 return a;
861}
862
863DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
864 TypeData * ftype = new TypeData( TypeData::Function );
865 ftype->function.params = params;
866 setBase( ftype );
867 return this;
868}
869
870static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
871 if ( type ) {
872 if ( type->kind != TypeData::Function ) {
873 type->base = addIdListToType( type->base, ids );
874 } else {
875 type->function.idList = ids;
876 } // if
877 return type;
878 } else {
879 TypeData * newtype = new TypeData( TypeData::Function );
880 newtype->function.idList = ids;
881 return newtype;
882 } // if
883} // addIdListToType
884
885DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
886 type = addIdListToType( type, ids );
887 return this;
888}
889
890DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
891 initializer = init;
892 return this;
893}
894
895DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
896 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
897 variable.initializer = init;
898 return this;
899}
900
901DeclarationNode * DeclarationNode::cloneType( string * newName ) {
902 DeclarationNode * newnode = new DeclarationNode;
903 newnode->type = maybeClone( type );
904 newnode->copySpecifiers( this );
905 assert( newName );
906 newnode->name = newName;
907 return newnode;
908}
909
910DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
911 if ( ! o ) return nullptr;
912
913 o->copySpecifiers( this );
914 if ( type ) {
915 TypeData * srcType = type;
916
917 // search for the base type by scanning off pointers and array designators
918 while ( srcType->base ) {
919 srcType = srcType->base;
920 } // while
921
922 TypeData * newType = srcType->clone();
923 if ( newType->kind == TypeData::AggregateInst ) {
924 // don't duplicate members
925 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
926 delete newType->aggInst.aggregate->enumeration.constants;
927 newType->aggInst.aggregate->enumeration.constants = nullptr;
928 newType->aggInst.aggregate->enumeration.body = false;
929 } else {
930 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
931 delete newType->aggInst.aggregate->aggregate.fields;
932 newType->aggInst.aggregate->aggregate.fields = nullptr;
933 newType->aggInst.aggregate->aggregate.body = false;
934 } // if
935 // don't hoist twice
936 newType->aggInst.hoistType = false;
937 } // if
938
939 newType->forall = maybeClone( type->forall );
940 if ( ! o->type ) {
941 o->type = newType;
942 } else {
943 addTypeToType( newType, o->type );
944 delete newType;
945 } // if
946 } // if
947 return o;
948}
949
950DeclarationNode * DeclarationNode::extractAggregate() const {
951 if ( type ) {
952 TypeData * ret = typeextractAggregate( type );
953 if ( ret ) {
954 DeclarationNode * newnode = new DeclarationNode;
955 newnode->type = ret;
956 return newnode;
957 } // if
958 } // if
959 return nullptr;
960}
961
962void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
963 SemanticErrorException errors;
964 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
965
966 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
967 try {
968 if ( DeclarationNode * extr = cur->extractAggregate() ) {
969 // handle the case where a structure declaration is contained within an object or type declaration
970 Declaration * decl = extr->build();
971 if ( decl ) {
972 decl->location = cur->location;
973 * out++ = decl;
974 } // if
975 delete extr;
976 } // if
977
978 Declaration * decl = cur->build();
979 if ( decl ) {
980 decl->location = cur->location;
981 * out++ = decl;
982 } // if
983 } catch( SemanticErrorException &e ) {
984 errors.append( e );
985 } // try
986 } // while
987
988 if ( ! errors.isEmpty() ) {
989 throw errors;
990 } // if
991} // buildList
992
993void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
994 SemanticErrorException errors;
995 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
996
997 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
998 try {
999 Declaration * decl = cur->build();
1000 if ( decl ) {
1001 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
1002 dwt->location = cur->location;
1003 * out++ = dwt;
1004 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1005 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
1006 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1007 obj->location = cur->location;
1008 * out++ = obj;
1009 delete agg;
1010 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1011 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
1012 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1013 obj->location = cur->location;
1014 * out++ = obj;
1015 } // if
1016 } // if
1017 } catch( SemanticErrorException &e ) {
1018 errors.append( e );
1019 } // try
1020 } // for
1021
1022 if ( ! errors.isEmpty() ) {
1023 throw errors;
1024 } // if
1025} // buildList
1026
1027void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1028 SemanticErrorException errors;
1029 std::back_insert_iterator< std::list< Type * > > out( outputList );
1030 const DeclarationNode * cur = firstNode;
1031
1032 while ( cur ) {
1033 try {
1034 * out++ = cur->buildType();
1035 } catch( SemanticErrorException &e ) {
1036 errors.append( e );
1037 } // try
1038 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1039 } // while
1040
1041 if ( ! errors.isEmpty() ) {
1042 throw errors;
1043 } // if
1044} // buildTypeList
1045
1046Declaration * DeclarationNode::build() const {
1047 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1048
1049 if ( asmStmt ) {
1050 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1051 } // if
1052
1053 if ( variable.tyClass != NoTypeClass ) {
1054 // otype is internally converted to dtype + otype parameters
1055 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1056 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1057 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1058 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1059 buildList( variable.assertions, ret->get_assertions() );
1060 return ret;
1061 } // if
1062
1063 if ( type ) {
1064 // Function specifiers can only appear on a function definition/declaration.
1065 //
1066 // inline _Noreturn int f(); // allowed
1067 // inline _Noreturn int g( int i ); // allowed
1068 // inline _Noreturn int i; // disallowed
1069 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1070 SemanticError( this, "invalid function specifier for " );
1071 } // if
1072 bool isDelete = initializer && initializer->get_isDelete();
1073 Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1074 if ( isDelete ) {
1075 DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl );
1076 dwt->isDeleted = true;
1077 }
1078 return decl;
1079 } // if
1080
1081 if ( assert.condition ) {
1082 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1083 }
1084
1085 // SUE's cannot have function specifiers, either
1086 //
1087 // inlne _Noreturn struct S { ... }; // disallowed
1088 // inlne _Noreturn enum E { ... }; // disallowed
1089 if ( funcSpecs.any() ) {
1090 SemanticError( this, "invalid function specifier for " );
1091 } // if
1092 assertf( name, "ObjectDecl must a have name\n" );
1093 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1094}
1095
1096Type * DeclarationNode::buildType() const {
1097 assert( type );
1098
1099 if ( attr.expr ) {
1100 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1101 } else if ( attr.type ) {
1102 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1103 } // if
1104
1105 switch ( type->kind ) {
1106 case TypeData::Enum:
1107 case TypeData::Aggregate: {
1108 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1109 buildList( type->aggregate.actuals, ret->get_parameters() );
1110 return ret;
1111 }
1112 case TypeData::Symbolic: {
1113 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1114 buildList( type->symbolic.actuals, ret->get_parameters() );
1115 return ret;
1116 }
1117 default:
1118 Type * simpletypes = typebuild( type );
1119 simpletypes->get_attributes() = attributes; // copy because member is const
1120 return simpletypes;
1121 } // switch
1122}
1123
1124// Local Variables: //
1125// tab-width: 4 //
1126// mode: c++ //
1127// compile-command: "make install" //
1128// End: //
Note: See TracBrowser for help on using the repository browser.