source: src/Parser/DeclarationNode.cc@ a8615fd1

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 a8615fd1 was 284da8c, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

cleanup, fix distribution order, add generic declaration/instantiation, extra qualifier check

  • 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 : Fri Jul 6 06:56:08 2018
13// Update Count : 1088
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 ( dst->base ) {
507 addQualifiersToType( src, dst->base );
508 } else if ( dst->kind == TypeData::Function ) {
509 dst->base = src;
510 src = nullptr;
511 } else {
512 dst->qualifiers |= src->qualifiers;
513 } // if
514} // addQualifiersToType
515
516DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
517 if ( ! q ) { return this; } // empty qualifier
518
519 checkSpecifiers( q );
520 copySpecifiers( q );
521
522 if ( ! q->type ) { delete q; return this; }
523
524 if ( ! type ) {
525 type = q->type; // reuse structure
526 q->type = nullptr;
527 delete q;
528 return this;
529 } // if
530
531 if ( q->type->forall ) { // forall qualifier ?
532 if ( type->forall ) { // polymorphic routine ?
533 type->forall->appendList( q->type->forall ); // augment forall qualifier
534 } else {
535 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
536 if ( type->aggregate.params ) { // polymorphic ?
537 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
538 } else { // not polymorphic
539 type->aggregate.params = q->type->forall; // set forall qualifier
540 } // if
541 } else { // not polymorphic
542 type->forall = q->type->forall; // make polymorphic routine
543 } // if
544 } // if
545 q->type->forall = nullptr; // forall qualifier moved
546 } // if
547
548 checkQualifiers( type, q->type );
549 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
550 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
551 } // if
552 addQualifiersToType( q->type, type );
553
554 delete q;
555 return this;
556} // addQualifiers
557
558static void addTypeToType( TypeData *&src, TypeData *&dst ) {
559 if ( src->forall && dst->kind == TypeData::Function ) {
560 if ( dst->forall ) {
561 dst->forall->appendList( src->forall );
562 } else {
563 dst->forall = src->forall;
564 } // if
565 src->forall = nullptr;
566 } // if
567 if ( dst->base ) {
568 addTypeToType( src, dst->base );
569 } else {
570 switch ( dst->kind ) {
571 case TypeData::Unknown:
572 src->qualifiers |= dst->qualifiers;
573 dst = src;
574 src = nullptr;
575 break;
576 case TypeData::Basic:
577 dst->qualifiers |= src->qualifiers;
578 if ( src->kind != TypeData::Unknown ) {
579 assert( src->kind == TypeData::Basic );
580
581 if ( dst->basictype == DeclarationNode::NoBasicType ) {
582 dst->basictype = src->basictype;
583 } else if ( src->basictype != DeclarationNode::NoBasicType )
584 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
585
586 if ( dst->complextype == DeclarationNode::NoComplexType ) {
587 dst->complextype = src->complextype;
588 } else if ( src->complextype != DeclarationNode::NoComplexType )
589 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
590
591 if ( dst->signedness == DeclarationNode::NoSignedness ) {
592 dst->signedness = src->signedness;
593 } else if ( src->signedness != DeclarationNode::NoSignedness )
594 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
595
596 if ( dst->length == DeclarationNode::NoLength ) {
597 dst->length = src->length;
598 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
599 dst->length = DeclarationNode::LongLong;
600 } else if ( src->length != DeclarationNode::NoLength )
601 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
602 } // if
603 break;
604 default:
605 switch ( src->kind ) {
606 case TypeData::Aggregate:
607 case TypeData::Enum:
608 dst->base = new TypeData( TypeData::AggregateInst );
609 dst->base->aggInst.aggregate = src;
610 if ( src->kind == TypeData::Aggregate ) {
611 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
612 } // if
613 dst->base->qualifiers |= src->qualifiers;
614 src = nullptr;
615 break;
616 default:
617 if ( dst->forall ) {
618 dst->forall->appendList( src->forall );
619 } else {
620 dst->forall = src->forall;
621 } // if
622 src->forall = nullptr;
623 dst->base = src;
624 src = nullptr;
625 } // switch
626 } // switch
627 } // if
628}
629
630DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
631 if ( o ) {
632 checkSpecifiers( o );
633 copySpecifiers( o );
634 if ( o->type ) {
635 if ( ! type ) {
636 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
637 type = new TypeData( TypeData::AggregateInst );
638 type->aggInst.aggregate = o->type;
639 if ( o->type->kind == TypeData::Aggregate ) {
640 type->aggInst.hoistType = o->type->aggregate.body;
641 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
642 } else {
643 type->aggInst.hoistType = o->type->enumeration.body;
644 } // if
645 type->qualifiers |= o->type->qualifiers;
646 } else {
647 type = o->type;
648 } // if
649 o->type = nullptr;
650 } else {
651 addTypeToType( o->type, type );
652 } // if
653 } // if
654 if ( o->bitfieldWidth ) {
655 bitfieldWidth = o->bitfieldWidth;
656 } // if
657
658 // there may be typedefs chained onto the type
659 if ( o->get_next() ) {
660 set_last( o->get_next()->clone() );
661 } // if
662 } // if
663 delete o;
664 return this;
665}
666
667DeclarationNode * DeclarationNode::addTypedef() {
668 TypeData * newtype = new TypeData( TypeData::Symbolic );
669 newtype->symbolic.params = nullptr;
670 newtype->symbolic.isTypedef = true;
671 newtype->symbolic.name = name ? new string( *name ) : nullptr;
672 newtype->base = type;
673 type = newtype;
674 return this;
675}
676
677DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
678 if ( variable.tyClass != NoTypeClass ) {
679 if ( variable.assertions ) {
680 variable.assertions->appendList( assertions );
681 } else {
682 variable.assertions = assertions;
683 } // if
684 return this;
685 } // if
686
687 assert( type );
688 switch ( type->kind ) {
689 case TypeData::Symbolic:
690 if ( type->symbolic.assertions ) {
691 type->symbolic.assertions->appendList( assertions );
692 } else {
693 type->symbolic.assertions = assertions;
694 } // if
695 break;
696 default:
697 assert( false );
698 } // switch
699
700 return this;
701}
702
703DeclarationNode * DeclarationNode::addName( string * newname ) {
704 assert( ! name );
705 name = newname;
706 return this;
707}
708
709DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
710 assert( ! asmName );
711 asmName = newname ? newname->asmName : nullptr;
712 return this->addQualifiers( newname );
713}
714
715DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
716 bitfieldWidth = size;
717 return this;
718}
719
720DeclarationNode * DeclarationNode::addVarArgs() {
721 assert( type );
722 hasEllipsis = true;
723 return this;
724}
725
726DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
727 assert( type );
728 assert( type->kind == TypeData::Function );
729 assert( ! type->function.body );
730 type->function.body = body;
731 type->function.withExprs = withExprs;
732 return this;
733}
734
735DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
736 assert( type );
737 assert( type->kind == TypeData::Function );
738 assert( ! type->function.oldDeclList );
739 type->function.oldDeclList = list;
740 return this;
741}
742
743DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
744 if ( type ) {
745 TypeData * prevBase = type;
746 TypeData * curBase = type->base;
747 while ( curBase != nullptr ) {
748 prevBase = curBase;
749 curBase = curBase->base;
750 } // while
751 prevBase->base = newType;
752 } else {
753 type = newType;
754 } // if
755 return this;
756}
757
758DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
759 if ( a ) {
760 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
761 attributes.push_front( attr );
762 } // for
763 a->attributes.clear();
764 } // if
765 return this;
766} // copyAttribute
767
768DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
769 if ( p ) {
770 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
771 setBase( p->type );
772 p->type = nullptr;
773 copyAttribute( p );
774 delete p;
775 } // if
776 return this;
777}
778
779DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
780 if ( a ) {
781 assert( a->type->kind == TypeData::Array );
782 setBase( a->type );
783 a->type = nullptr;
784 copyAttribute( a );
785 delete a;
786 } // if
787 return this;
788}
789
790DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
791 if ( p ) {
792 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
793 if ( type ) {
794 switch ( type->kind ) {
795 case TypeData::Aggregate:
796 case TypeData::Enum:
797 p->type->base = new TypeData( TypeData::AggregateInst );
798 p->type->base->aggInst.aggregate = type;
799 if ( type->kind == TypeData::Aggregate ) {
800 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
801 } // if
802 p->type->base->qualifiers |= type->qualifiers;
803 break;
804
805 default:
806 p->type->base = type;
807 } // switch
808 type = nullptr;
809 } // if
810 delete this;
811 return p;
812 } else {
813 return this;
814 } // if
815}
816
817static TypeData * findLast( TypeData * a ) {
818 assert( a );
819 TypeData * cur = a;
820 while ( cur->base ) {
821 cur = cur->base;
822 } // while
823 return cur;
824}
825
826DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
827 if ( ! a ) return this;
828 assert( a->type->kind == TypeData::Array );
829 TypeData * lastArray = findLast( a->type );
830 if ( type ) {
831 switch ( type->kind ) {
832 case TypeData::Aggregate:
833 case TypeData::Enum:
834 lastArray->base = new TypeData( TypeData::AggregateInst );
835 lastArray->base->aggInst.aggregate = type;
836 if ( type->kind == TypeData::Aggregate ) {
837 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
838 } // if
839 lastArray->base->qualifiers |= type->qualifiers;
840 break;
841 default:
842 lastArray->base = type;
843 } // switch
844 type = nullptr;
845 } // if
846 delete this;
847 return a;
848}
849
850DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
851 TypeData * ftype = new TypeData( TypeData::Function );
852 ftype->function.params = params;
853 setBase( ftype );
854 return this;
855}
856
857static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
858 if ( type ) {
859 if ( type->kind != TypeData::Function ) {
860 type->base = addIdListToType( type->base, ids );
861 } else {
862 type->function.idList = ids;
863 } // if
864 return type;
865 } else {
866 TypeData * newtype = new TypeData( TypeData::Function );
867 newtype->function.idList = ids;
868 return newtype;
869 } // if
870} // addIdListToType
871
872DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
873 type = addIdListToType( type, ids );
874 return this;
875}
876
877DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
878 initializer = init;
879 return this;
880}
881
882DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
883 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
884 variable.initializer = init;
885 return this;
886}
887
888DeclarationNode * DeclarationNode::cloneType( string * newName ) {
889 DeclarationNode * newnode = new DeclarationNode;
890 newnode->type = maybeClone( type );
891 newnode->copySpecifiers( this );
892 assert( newName );
893 newnode->name = newName;
894 return newnode;
895}
896
897DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
898 if ( ! o ) return nullptr;
899
900 o->copySpecifiers( this );
901 if ( type ) {
902 TypeData * srcType = type;
903
904 // search for the base type by scanning off pointers and array designators
905 while ( srcType->base ) {
906 srcType = srcType->base;
907 } // while
908
909 TypeData * newType = srcType->clone();
910 if ( newType->kind == TypeData::AggregateInst ) {
911 // don't duplicate members
912 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
913 delete newType->aggInst.aggregate->enumeration.constants;
914 newType->aggInst.aggregate->enumeration.constants = nullptr;
915 newType->aggInst.aggregate->enumeration.body = false;
916 } else {
917 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
918 delete newType->aggInst.aggregate->aggregate.fields;
919 newType->aggInst.aggregate->aggregate.fields = nullptr;
920 newType->aggInst.aggregate->aggregate.body = false;
921 } // if
922 // don't hoist twice
923 newType->aggInst.hoistType = false;
924 } // if
925
926 newType->forall = maybeClone( type->forall );
927 if ( ! o->type ) {
928 o->type = newType;
929 } else {
930 addTypeToType( newType, o->type );
931 delete newType;
932 } // if
933 } // if
934 return o;
935}
936
937DeclarationNode * DeclarationNode::extractAggregate() const {
938 if ( type ) {
939 TypeData * ret = typeextractAggregate( type );
940 if ( ret ) {
941 DeclarationNode * newnode = new DeclarationNode;
942 newnode->type = ret;
943 return newnode;
944 } // if
945 } // if
946 return nullptr;
947}
948
949void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
950 SemanticErrorException errors;
951 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
952
953 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
954 try {
955 if ( DeclarationNode * extr = cur->extractAggregate() ) {
956 // handle the case where a structure declaration is contained within an object or type declaration
957 Declaration * decl = extr->build();
958 if ( decl ) {
959 decl->location = cur->location;
960 * out++ = decl;
961 } // if
962 delete extr;
963 } // if
964
965 Declaration * decl = cur->build();
966 if ( decl ) {
967 decl->location = cur->location;
968 * out++ = decl;
969 } // if
970 } catch( SemanticErrorException &e ) {
971 errors.append( e );
972 } // try
973 } // while
974
975 if ( ! errors.isEmpty() ) {
976 throw errors;
977 } // if
978} // buildList
979
980void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
981 SemanticErrorException errors;
982 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
983
984 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
985 try {
986 Declaration * decl = cur->build();
987 if ( decl ) {
988 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
989 dwt->location = cur->location;
990 * out++ = dwt;
991 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
992 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
993 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
994 obj->location = cur->location;
995 * out++ = obj;
996 delete agg;
997 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
998 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
999 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1000 obj->location = cur->location;
1001 * out++ = obj;
1002 } // if
1003 } // if
1004 } catch( SemanticErrorException &e ) {
1005 errors.append( e );
1006 } // try
1007 } // for
1008
1009 if ( ! errors.isEmpty() ) {
1010 throw errors;
1011 } // if
1012} // buildList
1013
1014void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1015 SemanticErrorException errors;
1016 std::back_insert_iterator< std::list< Type * > > out( outputList );
1017 const DeclarationNode * cur = firstNode;
1018
1019 while ( cur ) {
1020 try {
1021 * out++ = cur->buildType();
1022 } catch( SemanticErrorException &e ) {
1023 errors.append( e );
1024 } // try
1025 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1026 } // while
1027
1028 if ( ! errors.isEmpty() ) {
1029 throw errors;
1030 } // if
1031} // buildTypeList
1032
1033Declaration * DeclarationNode::build() const {
1034 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1035
1036 if ( asmStmt ) {
1037 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1038 } // if
1039
1040 if ( variable.tyClass != NoTypeClass ) {
1041 // otype is internally converted to dtype + otype parameters
1042 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1043 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1044 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1045 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1046 buildList( variable.assertions, ret->get_assertions() );
1047 return ret;
1048 } // if
1049
1050 if ( type ) {
1051 // Function specifiers can only appear on a function definition/declaration.
1052 //
1053 // inline _Noreturn int f(); // allowed
1054 // inline _Noreturn int g( int i ); // allowed
1055 // inline _Noreturn int i; // disallowed
1056 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1057 SemanticError( this, "invalid function specifier for " );
1058 } // if
1059 // Forall qualifier can only appear on a function/aggregate definition/declaration.
1060 //
1061 // forall int f(); // allowed
1062 // forall int g( int i ); // allowed
1063 // forall int i; // disallowed
1064 if ( type->kind != TypeData::Function && type->forall ) {
1065 SemanticError( this, "invalid type qualifier 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.