source: src/Parser/DeclarationNode.cc@ 5bdeb35

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 5bdeb35 was a1c9ddd, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

remove changeKind, formatting

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