source: src/Parser/DeclarationNode.cc@ c653b37

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 c653b37 was c194661, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Reorganize QualifiedType node

  • Property mode set to 100644
File size: 37.6 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::newFromGlobalScope() {
257 DeclarationNode * newnode = new DeclarationNode;
258 newnode->type = new TypeData( TypeData::GlobalScope );
259 return newnode;
260}
261
262DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
263 DeclarationNode * newnode = new DeclarationNode;
264 newnode->type = new TypeData( TypeData::Qualified );
265 newnode->type->qualified.parent = parent->type;
266 newnode->type->qualified.child = child->type;
267 parent->type = nullptr;
268 child->type = nullptr;
269 delete parent;
270 delete child;
271 return newnode;
272}
273
274DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
275 assert( name );
276 DeclarationNode * newnode = new DeclarationNode;
277 newnode->type = new TypeData( TypeData::Aggregate );
278 newnode->type->aggregate.kind = kind;
279 newnode->type->aggregate.name = name;
280 newnode->type->aggregate.actuals = actuals;
281 newnode->type->aggregate.fields = fields;
282 newnode->type->aggregate.body = body;
283 newnode->type->aggregate.tagged = false;
284 newnode->type->aggregate.parent = nullptr;
285 return newnode;
286} // DeclarationNode::newAggregate
287
288DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
289 assert( name );
290 DeclarationNode * newnode = new DeclarationNode;
291 newnode->type = new TypeData( TypeData::Enum );
292 newnode->type->enumeration.name = name;
293 newnode->type->enumeration.constants = constants;
294 newnode->type->enumeration.body = body;
295 return newnode;
296} // DeclarationNode::newEnum
297
298DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
299 DeclarationNode * newnode = new DeclarationNode;
300 newnode->name = name;
301 newnode->enumeratorValue.reset( constant );
302 return newnode;
303} // DeclarationNode::newEnumConstant
304
305DeclarationNode * DeclarationNode::newName( const string * name ) {
306 DeclarationNode * newnode = new DeclarationNode;
307 newnode->name = name;
308 return newnode;
309} // DeclarationNode::newName
310
311DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
312 DeclarationNode * newnode = new DeclarationNode;
313 newnode->type = new TypeData( TypeData::SymbolicInst );
314 newnode->type->symbolic.name = name;
315 newnode->type->symbolic.isTypedef = false;
316 newnode->type->symbolic.actuals = params;
317 return newnode;
318} // DeclarationNode::newFromTypeGen
319
320DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, const string * name ) {
321 DeclarationNode * newnode = new DeclarationNode;
322 newnode->type = nullptr;
323 assert( ! newnode->name );
324// newnode->variable.name = name;
325 newnode->name = name;
326 newnode->variable.tyClass = tc;
327 newnode->variable.assertions = nullptr;
328 return newnode;
329} // DeclarationNode::newTypeParam
330
331DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
332 DeclarationNode * newnode = new DeclarationNode;
333 newnode->type = new TypeData( TypeData::Aggregate );
334 newnode->type->aggregate.name = name;
335 newnode->type->aggregate.kind = Trait;
336 newnode->type->aggregate.params = params;
337 newnode->type->aggregate.fields = asserts;
338 return newnode;
339} // DeclarationNode::newTrait
340
341DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
342 DeclarationNode * newnode = new DeclarationNode;
343 newnode->type = new TypeData( TypeData::AggregateInst );
344 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
345 newnode->type->aggInst.aggregate->aggregate.kind = Trait;
346 newnode->type->aggInst.aggregate->aggregate.name = name;
347 newnode->type->aggInst.params = params;
348 return newnode;
349} // DeclarationNode::newTraitUse
350
351DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
352 DeclarationNode * newnode = new DeclarationNode;
353 newnode->name = name;
354 newnode->type = new TypeData( TypeData::Symbolic );
355 newnode->type->symbolic.isTypedef = false;
356 newnode->type->symbolic.params = typeParams;
357 return newnode;
358} // DeclarationNode::newTypeDecl
359
360DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
361 DeclarationNode * newnode = new DeclarationNode;
362 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
363 if ( kind == OperKinds::And ) {
364 // T && is parsed as 'And' operator rather than two references => add a second reference type
365 TypeData * td = new TypeData( TypeData::Reference );
366 td->base = newnode->type;
367 newnode->type = td;
368 }
369 if ( qualifiers ) {
370 return newnode->addQualifiers( qualifiers );
371 } else {
372 return newnode;
373 } // if
374} // DeclarationNode::newPointer
375
376DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
377 DeclarationNode * newnode = new DeclarationNode;
378 newnode->type = new TypeData( TypeData::Array );
379 newnode->type->array.dimension = size;
380 newnode->type->array.isStatic = isStatic;
381 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
382 newnode->type->array.isVarLen = false;
383 } else {
384 newnode->type->array.isVarLen = true;
385 } // if
386 return newnode->addQualifiers( qualifiers );
387} // DeclarationNode::newArray
388
389DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
390 DeclarationNode * newnode = new DeclarationNode;
391 newnode->type = new TypeData( TypeData::Array );
392 newnode->type->array.dimension = nullptr;
393 newnode->type->array.isStatic = false;
394 newnode->type->array.isVarLen = true;
395 return newnode->addQualifiers( qualifiers );
396}
397
398DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
399 DeclarationNode * newnode = new DeclarationNode;
400 newnode->bitfieldWidth = size;
401 return newnode;
402}
403
404DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
405 DeclarationNode * newnode = new DeclarationNode;
406 newnode->type = new TypeData( TypeData::Tuple );
407 newnode->type->tuple = members;
408 return newnode;
409}
410
411DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
412 DeclarationNode * newnode = new DeclarationNode;
413 newnode->type = new TypeData( TypeData::Typeof );
414 newnode->type->typeexpr = expr;
415 return newnode;
416}
417
418DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
419 DeclarationNode * newnode = new DeclarationNode;
420 newnode->type = new TypeData( TypeData::Builtin );
421 newnode->builtin = bt;
422 newnode->type->builtintype = newnode->builtin;
423 return newnode;
424} // DeclarationNode::newBuiltinType
425
426DeclarationNode * DeclarationNode::newAttr( const string * name, ExpressionNode * expr ) {
427 DeclarationNode * newnode = new DeclarationNode;
428 newnode->type = nullptr;
429// newnode->attr.name = name;
430 newnode->name = name;
431 newnode->attr.expr = expr;
432 return newnode;
433}
434
435DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {
436 DeclarationNode * newnode = new DeclarationNode;
437 newnode->type = nullptr;
438// newnode->attr.name = name;
439 newnode->name = name;
440 newnode->attr.type = type;
441 return newnode;
442}
443
444DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
445 DeclarationNode * newnode = new DeclarationNode;
446 newnode->type = nullptr;
447 std::list< Expression * > exprs;
448 buildList( expr, exprs );
449 newnode->attributes.push_back( new Attribute( *name, exprs ) );
450 delete name;
451 return newnode;
452}
453
454DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
455 DeclarationNode * newnode = new DeclarationNode;
456 newnode->asmStmt = stmt;
457 return newnode;
458}
459
460DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) {
461 DeclarationNode * newnode = new DeclarationNode;
462 newnode->assert.condition = condition;
463 newnode->assert.message = message;
464 return newnode;
465}
466
467
468void appendError( string & dst, const string & src ) {
469 if ( src.empty() ) return;
470 if ( dst.empty() ) { dst = src; return; }
471 dst += ", " + src;
472} // appendError
473
474void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
475 const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
476
477 if ( (qsrc & qdst).any() ) { // duplicates ?
478 for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
479 if ( qsrc[i] && qdst[i] ) {
480 appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
481 } // if
482 } // for
483 } // for
484} // DeclarationNode::checkQualifiers
485
486void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
487 if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ?
488 for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
489 if ( funcSpecs[i] && src->funcSpecs[i] ) {
490 appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
491 } // if
492 } // for
493 } // if
494
495 if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
496 if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
497 for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
498 if ( storageClasses[i] && src->storageClasses[i] ) {
499 appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
500 } // if
501 } // for
502 // src is the new item being added and has a single bit
503 } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
504 appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
505 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
506 src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier
507 } // if
508 } // if
509
510 appendError( error, src->error );
511} // DeclarationNode::checkSpecifiers
512
513DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
514 funcSpecs |= q->funcSpecs;
515 storageClasses |= q->storageClasses;
516
517 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
518 attributes.push_front( attr->clone() );
519 } // for
520 return this;
521} // DeclarationNode::copySpecifiers
522
523static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
524 if ( src->forall && dst->kind == TypeData::Function ) {
525 if ( dst->forall ) {
526 dst->forall->appendList( src->forall );
527 } else {
528 dst->forall = src->forall;
529 } // if
530 src->forall = nullptr;
531 } // if
532 if ( dst->base ) {
533 addQualifiersToType( src, dst->base );
534 } else if ( dst->kind == TypeData::Function ) {
535 dst->base = src;
536 src = nullptr;
537 } else {
538 dst->qualifiers |= src->qualifiers;
539 } // if
540} // addQualifiersToType
541
542DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
543 if ( ! q ) { return this; } // empty qualifier
544
545 checkSpecifiers( q );
546 copySpecifiers( q );
547
548 if ( ! q->type ) { delete q; return this; }
549
550 if ( ! type ) {
551 type = q->type; // reuse structure
552 q->type = nullptr;
553 delete q;
554 return this;
555 } // if
556
557 if ( q->type->forall ) { // forall qualifier ?
558 if ( type->forall ) { // polymorphic routine ?
559 type->forall->appendList( q->type->forall ); // augment forall qualifier
560 } else {
561 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
562 if ( type->aggregate.params ) { // polymorphic ?
563 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
564 } else { // not polymorphic
565 type->aggregate.params = q->type->forall; // set forall qualifier
566 } // if
567 } else { // not polymorphic
568 type->forall = q->type->forall; // make polymorphic routine
569 } // if
570 } // if
571 q->type->forall = nullptr; // forall qualifier moved
572 } // if
573
574 checkQualifiers( type, q->type );
575 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
576 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
577 } // if
578 addQualifiersToType( q->type, type );
579
580 delete q;
581 return this;
582} // addQualifiers
583
584static void addTypeToType( TypeData *&src, TypeData *&dst ) {
585 if ( src->forall && dst->kind == TypeData::Function ) {
586 if ( dst->forall ) {
587 dst->forall->appendList( src->forall );
588 } else {
589 dst->forall = src->forall;
590 } // if
591 src->forall = nullptr;
592 } // if
593 if ( dst->base ) {
594 addTypeToType( src, dst->base );
595 } else {
596 switch ( dst->kind ) {
597 case TypeData::Unknown:
598 src->qualifiers |= dst->qualifiers;
599 dst = src;
600 src = nullptr;
601 break;
602 case TypeData::Basic:
603 dst->qualifiers |= src->qualifiers;
604 if ( src->kind != TypeData::Unknown ) {
605 assert( src->kind == TypeData::Basic );
606
607 if ( dst->basictype == DeclarationNode::NoBasicType ) {
608 dst->basictype = src->basictype;
609 } else if ( src->basictype != DeclarationNode::NoBasicType )
610 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
611
612 if ( dst->complextype == DeclarationNode::NoComplexType ) {
613 dst->complextype = src->complextype;
614 } else if ( src->complextype != DeclarationNode::NoComplexType )
615 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
616
617 if ( dst->signedness == DeclarationNode::NoSignedness ) {
618 dst->signedness = src->signedness;
619 } else if ( src->signedness != DeclarationNode::NoSignedness )
620 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
621
622 if ( dst->length == DeclarationNode::NoLength ) {
623 dst->length = src->length;
624 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
625 dst->length = DeclarationNode::LongLong;
626 } else if ( src->length != DeclarationNode::NoLength )
627 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
628 } // if
629 break;
630 default:
631 switch ( src->kind ) {
632 case TypeData::Aggregate:
633 case TypeData::Enum:
634 dst->base = new TypeData( TypeData::AggregateInst );
635 dst->base->aggInst.aggregate = src;
636 if ( src->kind == TypeData::Aggregate ) {
637 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
638 } // if
639 dst->base->qualifiers |= src->qualifiers;
640 src = nullptr;
641 break;
642 default:
643 if ( dst->forall ) {
644 dst->forall->appendList( src->forall );
645 } else {
646 dst->forall = src->forall;
647 } // if
648 src->forall = nullptr;
649 dst->base = src;
650 src = nullptr;
651 } // switch
652 } // switch
653 } // if
654}
655
656DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
657 if ( o ) {
658 checkSpecifiers( o );
659 copySpecifiers( o );
660 if ( o->type ) {
661 if ( ! type ) {
662 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
663 type = new TypeData( TypeData::AggregateInst );
664 type->aggInst.aggregate = o->type;
665 if ( o->type->kind == TypeData::Aggregate ) {
666 type->aggInst.hoistType = o->type->aggregate.body;
667 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
668 } else {
669 type->aggInst.hoistType = o->type->enumeration.body;
670 } // if
671 type->qualifiers |= o->type->qualifiers;
672 } else {
673 type = o->type;
674 } // if
675 o->type = nullptr;
676 } else {
677 addTypeToType( o->type, type );
678 } // if
679 } // if
680 if ( o->bitfieldWidth ) {
681 bitfieldWidth = o->bitfieldWidth;
682 } // if
683
684 // there may be typedefs chained onto the type
685 if ( o->get_next() ) {
686 set_last( o->get_next()->clone() );
687 } // if
688 } // if
689 delete o;
690 return this;
691}
692
693DeclarationNode * DeclarationNode::addTypedef() {
694 TypeData * newtype = new TypeData( TypeData::Symbolic );
695 newtype->symbolic.params = nullptr;
696 newtype->symbolic.isTypedef = true;
697 newtype->symbolic.name = name ? new string( *name ) : nullptr;
698 newtype->base = type;
699 type = newtype;
700 return this;
701}
702
703DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
704 if ( variable.tyClass != NoTypeClass ) {
705 if ( variable.assertions ) {
706 variable.assertions->appendList( assertions );
707 } else {
708 variable.assertions = assertions;
709 } // if
710 return this;
711 } // if
712
713 assert( type );
714 switch ( type->kind ) {
715 case TypeData::Symbolic:
716 if ( type->symbolic.assertions ) {
717 type->symbolic.assertions->appendList( assertions );
718 } else {
719 type->symbolic.assertions = assertions;
720 } // if
721 break;
722 default:
723 assert( false );
724 } // switch
725
726 return this;
727}
728
729DeclarationNode * DeclarationNode::addName( string * newname ) {
730 assert( ! name );
731 name = newname;
732 return this;
733}
734
735DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
736 assert( ! asmName );
737 asmName = newname ? newname->asmName : nullptr;
738 return this->addQualifiers( newname );
739}
740
741DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
742 bitfieldWidth = size;
743 return this;
744}
745
746DeclarationNode * DeclarationNode::addVarArgs() {
747 assert( type );
748 hasEllipsis = true;
749 return this;
750}
751
752DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
753 assert( type );
754 assert( type->kind == TypeData::Function );
755 assert( ! type->function.body );
756 type->function.body = body;
757 type->function.withExprs = withExprs;
758 return this;
759}
760
761DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
762 assert( type );
763 assert( type->kind == TypeData::Function );
764 assert( ! type->function.oldDeclList );
765 type->function.oldDeclList = list;
766 return this;
767}
768
769DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
770 if ( type ) {
771 TypeData * prevBase = type;
772 TypeData * curBase = type->base;
773 while ( curBase != nullptr ) {
774 prevBase = curBase;
775 curBase = curBase->base;
776 } // while
777 prevBase->base = newType;
778 } else {
779 type = newType;
780 } // if
781 return this;
782}
783
784DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
785 if ( a ) {
786 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
787 attributes.push_front( attr );
788 } // for
789 a->attributes.clear();
790 } // if
791 return this;
792} // copyAttribute
793
794DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
795 if ( p ) {
796 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
797 setBase( p->type );
798 p->type = nullptr;
799 copyAttribute( p );
800 delete p;
801 } // if
802 return this;
803}
804
805DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
806 if ( a ) {
807 assert( a->type->kind == TypeData::Array );
808 setBase( a->type );
809 a->type = nullptr;
810 copyAttribute( a );
811 delete a;
812 } // if
813 return this;
814}
815
816DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
817 if ( p ) {
818 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
819 if ( type ) {
820 switch ( type->kind ) {
821 case TypeData::Aggregate:
822 case TypeData::Enum:
823 p->type->base = new TypeData( TypeData::AggregateInst );
824 p->type->base->aggInst.aggregate = type;
825 if ( type->kind == TypeData::Aggregate ) {
826 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
827 } // if
828 p->type->base->qualifiers |= type->qualifiers;
829 break;
830
831 default:
832 p->type->base = type;
833 } // switch
834 type = nullptr;
835 } // if
836 delete this;
837 return p;
838 } else {
839 return this;
840 } // if
841}
842
843static TypeData * findLast( TypeData * a ) {
844 assert( a );
845 TypeData * cur = a;
846 while ( cur->base ) {
847 cur = cur->base;
848 } // while
849 return cur;
850}
851
852DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
853 if ( ! a ) return this;
854 assert( a->type->kind == TypeData::Array );
855 TypeData * lastArray = findLast( a->type );
856 if ( type ) {
857 switch ( type->kind ) {
858 case TypeData::Aggregate:
859 case TypeData::Enum:
860 lastArray->base = new TypeData( TypeData::AggregateInst );
861 lastArray->base->aggInst.aggregate = type;
862 if ( type->kind == TypeData::Aggregate ) {
863 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
864 } // if
865 lastArray->base->qualifiers |= type->qualifiers;
866 break;
867 default:
868 lastArray->base = type;
869 } // switch
870 type = nullptr;
871 } // if
872 delete this;
873 return a;
874}
875
876DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
877 TypeData * ftype = new TypeData( TypeData::Function );
878 ftype->function.params = params;
879 setBase( ftype );
880 return this;
881}
882
883static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
884 if ( type ) {
885 if ( type->kind != TypeData::Function ) {
886 type->base = addIdListToType( type->base, ids );
887 } else {
888 type->function.idList = ids;
889 } // if
890 return type;
891 } else {
892 TypeData * newtype = new TypeData( TypeData::Function );
893 newtype->function.idList = ids;
894 return newtype;
895 } // if
896} // addIdListToType
897
898DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
899 type = addIdListToType( type, ids );
900 return this;
901}
902
903DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
904 initializer = init;
905 return this;
906}
907
908DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
909 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
910 variable.initializer = init;
911 return this;
912}
913
914DeclarationNode * DeclarationNode::cloneType( string * newName ) {
915 DeclarationNode * newnode = new DeclarationNode;
916 newnode->type = maybeClone( type );
917 newnode->copySpecifiers( this );
918 assert( newName );
919 newnode->name = newName;
920 return newnode;
921}
922
923DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
924 if ( ! o ) return nullptr;
925
926 o->copySpecifiers( this );
927 if ( type ) {
928 TypeData * srcType = type;
929
930 // search for the base type by scanning off pointers and array designators
931 while ( srcType->base ) {
932 srcType = srcType->base;
933 } // while
934
935 TypeData * newType = srcType->clone();
936 if ( newType->kind == TypeData::AggregateInst ) {
937 // don't duplicate members
938 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
939 delete newType->aggInst.aggregate->enumeration.constants;
940 newType->aggInst.aggregate->enumeration.constants = nullptr;
941 newType->aggInst.aggregate->enumeration.body = false;
942 } else {
943 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
944 delete newType->aggInst.aggregate->aggregate.fields;
945 newType->aggInst.aggregate->aggregate.fields = nullptr;
946 newType->aggInst.aggregate->aggregate.body = false;
947 } // if
948 // don't hoist twice
949 newType->aggInst.hoistType = false;
950 } // if
951
952 newType->forall = maybeClone( type->forall );
953 if ( ! o->type ) {
954 o->type = newType;
955 } else {
956 addTypeToType( newType, o->type );
957 delete newType;
958 } // if
959 } // if
960 return o;
961}
962
963DeclarationNode * DeclarationNode::extractAggregate() const {
964 if ( type ) {
965 TypeData * ret = typeextractAggregate( type );
966 if ( ret ) {
967 DeclarationNode * newnode = new DeclarationNode;
968 newnode->type = ret;
969 return newnode;
970 } // if
971 } // if
972 return nullptr;
973}
974
975void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
976 SemanticErrorException errors;
977 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
978
979 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
980 try {
981 if ( DeclarationNode * extr = cur->extractAggregate() ) {
982 // handle the case where a structure declaration is contained within an object or type declaration
983 Declaration * decl = extr->build();
984 if ( decl ) {
985 decl->location = cur->location;
986 * out++ = decl;
987 } // if
988 delete extr;
989 } // if
990
991 Declaration * decl = cur->build();
992 if ( decl ) {
993 decl->location = cur->location;
994 * out++ = decl;
995 } // if
996 } catch( SemanticErrorException &e ) {
997 errors.append( e );
998 } // try
999 } // while
1000
1001 if ( ! errors.isEmpty() ) {
1002 throw errors;
1003 } // if
1004} // buildList
1005
1006void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
1007 SemanticErrorException errors;
1008 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
1009
1010 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
1011 try {
1012 Declaration * decl = cur->build();
1013 assert( decl );
1014 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
1015 dwt->location = cur->location;
1016 * out++ = dwt;
1017 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1018 // xxx - this might be where anonymous struct members are added - should be conditional on struct name
1019 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
1020 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1021 obj->location = cur->location;
1022 * out++ = obj;
1023 delete agg;
1024 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1025 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
1026 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1027 obj->location = cur->location;
1028 * out++ = obj;
1029 } // if
1030 } catch( SemanticErrorException &e ) {
1031 errors.append( e );
1032 } // try
1033 } // for
1034
1035 if ( ! errors.isEmpty() ) {
1036 throw errors;
1037 } // if
1038} // buildList
1039
1040void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1041 SemanticErrorException errors;
1042 std::back_insert_iterator< std::list< Type * > > out( outputList );
1043 const DeclarationNode * cur = firstNode;
1044
1045 while ( cur ) {
1046 try {
1047 * out++ = cur->buildType();
1048 } catch( SemanticErrorException &e ) {
1049 errors.append( e );
1050 } // try
1051 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1052 } // while
1053
1054 if ( ! errors.isEmpty() ) {
1055 throw errors;
1056 } // if
1057} // buildTypeList
1058
1059Declaration * DeclarationNode::build() const {
1060 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1061
1062 if ( asmStmt ) {
1063 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1064 } // if
1065
1066 if ( variable.tyClass != NoTypeClass ) {
1067 // otype is internally converted to dtype + otype parameters
1068 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1069 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1070 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1071 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1072 buildList( variable.assertions, ret->get_assertions() );
1073 return ret;
1074 } // if
1075
1076 if ( type ) {
1077 // Function specifiers can only appear on a function definition/declaration.
1078 //
1079 // inline _Noreturn int f(); // allowed
1080 // inline _Noreturn int g( int i ); // allowed
1081 // inline _Noreturn int i; // disallowed
1082 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1083 SemanticError( this, "invalid function specifier for " );
1084 } // if
1085 bool isDelete = initializer && initializer->get_isDelete();
1086 Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1087 if ( isDelete ) {
1088 DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl );
1089 dwt->isDeleted = true;
1090 }
1091 return decl;
1092 } // if
1093
1094 if ( assert.condition ) {
1095 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1096 }
1097
1098 // SUE's cannot have function specifiers, either
1099 //
1100 // inlne _Noreturn struct S { ... }; // disallowed
1101 // inlne _Noreturn enum E { ... }; // disallowed
1102 if ( funcSpecs.any() ) {
1103 SemanticError( this, "invalid function specifier for " );
1104 } // if
1105 assertf( name, "ObjectDecl must a have name\n" );
1106 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1107}
1108
1109Type * DeclarationNode::buildType() const {
1110 assert( type );
1111
1112 if ( attr.expr ) {
1113 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1114 } else if ( attr.type ) {
1115 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1116 } // if
1117
1118 switch ( type->kind ) {
1119 case TypeData::Enum:
1120 case TypeData::Aggregate: {
1121 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1122 buildList( type->aggregate.actuals, ret->get_parameters() );
1123 return ret;
1124 }
1125 case TypeData::Symbolic: {
1126 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1127 buildList( type->symbolic.actuals, ret->get_parameters() );
1128 return ret;
1129 }
1130 default:
1131 Type * simpletypes = typebuild( type );
1132 simpletypes->get_attributes() = attributes; // copy because member is const
1133 return simpletypes;
1134 } // switch
1135}
1136
1137// Local Variables: //
1138// tab-width: 4 //
1139// mode: c++ //
1140// compile-command: "make install" //
1141// End: //
Note: See TracBrowser for help on using the repository browser.