source: src/Parser/TypeData.cc@ 01ba701

ADT ast-experimental pthread-emulation
Last change on this file since 01ba701 was 66406f3, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

update debug printing

  • Property mode set to 100644
File size: 37.2 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// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue May 10 22:36:52 2022
13// Update Count : 677
14//
15
16#include <cassert> // for assert
17#include <ostream> // for operator<<, ostream, basic_ostream
18
19#include "Common/SemanticError.h" // for SemanticError
20#include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
21#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
22#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
23#include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
24#include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
25#include "SynTree/Statement.h" // for CompoundStmt, Statement
26#include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
27#include "TypeData.h"
28
29class Attribute;
30
31using namespace std;
32
33TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
34 switch ( kind ) {
35 case Unknown:
36 case Pointer:
37 case Reference:
38 case EnumConstant:
39 case GlobalScope:
40 // nothing else to initialize
41 break;
42 case Basic:
43 // basic = new Basic_t;
44 break;
45 case Array:
46 // array = new Array_t;
47 array.dimension = nullptr;
48 array.isVarLen = false;
49 array.isStatic = false;
50 break;
51 case Function:
52 // function = new Function_t;
53 function.params = nullptr;
54 function.idList = nullptr;
55 function.oldDeclList = nullptr;
56 function.body = nullptr;
57 function.withExprs = nullptr;
58 break;
59 // Enum is an Aggregate, so both structures are initialized together.
60 case Enum:
61 // enumeration = new Enumeration_t;
62 enumeration.name = nullptr;
63 enumeration.constants = nullptr;
64 enumeration.body = false;
65 enumeration.anon = false;
66 break;
67 case Aggregate:
68 // aggregate = new Aggregate_t;
69 aggregate.kind = AggregateDecl::NoAggregate;
70 aggregate.name = nullptr;
71 aggregate.params = nullptr;
72 aggregate.actuals = nullptr;
73 aggregate.fields = nullptr;
74 aggregate.body = false;
75 aggregate.tagged = false;
76 aggregate.parent = nullptr;
77 aggregate.anon = false;
78 break;
79 case AggregateInst:
80 // aggInst = new AggInst_t;
81 aggInst.aggregate = nullptr;
82 aggInst.params = nullptr;
83 aggInst.hoistType = false;
84 break;
85 case Symbolic:
86 case SymbolicInst:
87 // symbolic = new Symbolic_t;
88 symbolic.name = nullptr;
89 symbolic.params = nullptr;
90 symbolic.actuals = nullptr;
91 symbolic.assertions = nullptr;
92 break;
93 case Tuple:
94 // tuple = new Tuple_t;
95 tuple = nullptr;
96 break;
97 case Typeof:
98 case Basetypeof:
99 // typeexpr = new Typeof_t;
100 typeexpr = nullptr;
101 break;
102 case Vtable:
103 break;
104 case Builtin:
105 // builtin = new Builtin_t;
106 case Qualified:
107 qualified.parent = nullptr;
108 qualified.child = nullptr;
109 break;
110 } // switch
111} // TypeData::TypeData
112
113
114TypeData::~TypeData() {
115 delete base;
116 delete forall;
117
118 switch ( kind ) {
119 case Unknown:
120 case Pointer:
121 case Reference:
122 case EnumConstant:
123 case GlobalScope:
124 // nothing to destroy
125 break;
126 case Basic:
127 // delete basic;
128 break;
129 case Array:
130 delete array.dimension;
131 // delete array;
132 break;
133 case Function:
134 delete function.params;
135 delete function.idList;
136 delete function.oldDeclList;
137 delete function.body;
138 delete function.withExprs;
139 // delete function;
140 break;
141 case Aggregate:
142 delete aggregate.name;
143 delete aggregate.params;
144 delete aggregate.actuals;
145 delete aggregate.fields;
146 // delete aggregate;
147 break;
148 case AggregateInst:
149 delete aggInst.aggregate;
150 delete aggInst.params;
151 // delete aggInst;
152 break;
153 case Enum:
154 delete enumeration.name;
155 delete enumeration.constants;
156 // delete enumeration;
157 break;
158 case Symbolic:
159 case SymbolicInst:
160 delete symbolic.name;
161 delete symbolic.params;
162 delete symbolic.actuals;
163 delete symbolic.assertions;
164 // delete symbolic;
165 break;
166 case Tuple:
167 // delete tuple->members;
168 delete tuple;
169 break;
170 case Typeof:
171 case Basetypeof:
172 // delete typeexpr->expr;
173 delete typeexpr;
174 break;
175 case Vtable:
176 break;
177 case Builtin:
178 // delete builtin;
179 break;
180 case Qualified:
181 delete qualified.parent;
182 delete qualified.child;
183 } // switch
184} // TypeData::~TypeData
185
186
187TypeData * TypeData::clone() const {
188 TypeData * newtype = new TypeData( kind );
189 newtype->qualifiers = qualifiers;
190 newtype->base = maybeClone( base );
191 newtype->forall = maybeClone( forall );
192
193 switch ( kind ) {
194 case Unknown:
195 case EnumConstant:
196 case Pointer:
197 case Reference:
198 case GlobalScope:
199 // nothing else to copy
200 break;
201 case Basic:
202 newtype->basictype = basictype;
203 newtype->complextype = complextype;
204 newtype->signedness = signedness;
205 newtype->length = length;
206 break;
207 case Array:
208 newtype->array.dimension = maybeClone( array.dimension );
209 newtype->array.isVarLen = array.isVarLen;
210 newtype->array.isStatic = array.isStatic;
211 break;
212 case Function:
213 newtype->function.params = maybeClone( function.params );
214 newtype->function.idList = maybeClone( function.idList );
215 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
216 newtype->function.body = maybeClone( function.body );
217 newtype->function.withExprs = maybeClone( function.withExprs );
218 break;
219 case Aggregate:
220 newtype->aggregate.kind = aggregate.kind;
221 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
222 newtype->aggregate.params = maybeClone( aggregate.params );
223 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
224 newtype->aggregate.fields = maybeClone( aggregate.fields );
225 newtype->aggregate.body = aggregate.body;
226 newtype->aggregate.anon = aggregate.anon;
227 newtype->aggregate.tagged = aggregate.tagged;
228 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
229 break;
230 case AggregateInst:
231 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
232 newtype->aggInst.params = maybeClone( aggInst.params );
233 newtype->aggInst.hoistType = aggInst.hoistType;
234 break;
235 case Enum:
236 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
237 newtype->enumeration.constants = maybeClone( enumeration.constants );
238 newtype->enumeration.body = enumeration.body;
239 newtype->enumeration.anon = enumeration.anon;
240 break;
241 case Symbolic:
242 case SymbolicInst:
243 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
244 newtype->symbolic.params = maybeClone( symbolic.params );
245 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
246 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
247 newtype->symbolic.isTypedef = symbolic.isTypedef;
248 break;
249 case Tuple:
250 newtype->tuple = maybeClone( tuple );
251 break;
252 case Typeof:
253 case Basetypeof:
254 newtype->typeexpr = maybeClone( typeexpr );
255 break;
256 case Vtable:
257 break;
258 case Builtin:
259 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
260 newtype->builtintype = builtintype;
261 break;
262 case Qualified:
263 newtype->qualified.parent = maybeClone( qualified.parent );
264 newtype->qualified.child = maybeClone( qualified.child );
265 break;
266 } // switch
267 return newtype;
268} // TypeData::clone
269
270
271void TypeData::print( ostream &os, int indent ) const {
272 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
273 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
274 } // for
275
276 if ( forall ) {
277 os << "forall " << endl;
278 forall->printList( os, indent + 4 );
279 } // if
280
281 switch ( kind ) {
282 case Basic:
283 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
284 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
285 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
286 if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
287 break;
288 case Pointer:
289 os << "pointer ";
290 if ( base ) {
291 os << "to ";
292 base->print( os, indent );
293 } // if
294 break;
295 case Reference:
296 os << "reference ";
297 if ( base ) {
298 os << "to ";
299 base->print( os, indent );
300 } // if
301 break;
302 case Array:
303 if ( array.isStatic ) {
304 os << "static ";
305 } // if
306 if ( array.dimension ) {
307 os << "array of ";
308 array.dimension->printOneLine( os, indent );
309 } else if ( array.isVarLen ) {
310 os << "variable-length array of ";
311 } else {
312 os << "open array of ";
313 } // if
314 if ( base ) {
315 base->print( os, indent );
316 } // if
317 break;
318 case Function:
319 os << "function" << endl;
320 if ( function.params ) {
321 os << string( indent + 2, ' ' ) << "with parameters " << endl;
322 function.params->printList( os, indent + 4 );
323 } else {
324 os << string( indent + 2, ' ' ) << "with no parameters" << endl;
325 } // if
326 if ( function.idList ) {
327 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
328 function.idList->printList( os, indent + 4 );
329 } // if
330 if ( function.oldDeclList ) {
331 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
332 function.oldDeclList->printList( os, indent + 4 );
333 } // if
334 os << string( indent + 2, ' ' ) << "returning ";
335 if ( base ) {
336 base->print( os, indent + 4 );
337 } else {
338 os << "nothing ";
339 } // if
340 os << endl;
341 if ( function.body ) {
342 os << string( indent + 2, ' ' ) << "with body " << endl;
343 function.body->printList( os, indent + 2 );
344 } // if
345 break;
346 case Aggregate:
347 os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
348 if ( aggregate.params ) {
349 os << string( indent + 2, ' ' ) << "with type parameters" << endl;
350 aggregate.params->printList( os, indent + 4 );
351 } // if
352 if ( aggregate.actuals ) {
353 os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
354 aggregate.actuals->printList( os, indent + 4 );
355 } // if
356 if ( aggregate.fields ) {
357 os << string( indent + 2, ' ' ) << "with members" << endl;
358 aggregate.fields->printList( os, indent + 4 );
359 } // if
360 if ( aggregate.body ) {
361 os << string( indent + 2, ' ' ) << " with body" << endl;
362 } // if
363 break;
364 case AggregateInst:
365 if ( aggInst.aggregate ) {
366 os << "instance of " ;
367 aggInst.aggregate->print( os, indent );
368 } else {
369 os << "instance of an unspecified aggregate ";
370 } // if
371 if ( aggInst.params ) {
372 os << string( indent + 2, ' ' ) << "with parameters" << endl;
373 aggInst.params->printList( os, indent + 2 );
374 } // if
375 break;
376 case Enum:
377 os << "enumeration ";
378 if ( enumeration.constants ) {
379 os << "with constants" << endl;
380 enumeration.constants->printList( os, indent + 2 );
381 } // if
382 if ( enumeration.body ) {
383 os << string( indent + 2, ' ' ) << " with body" << endl;
384 } // if
385 if ( base ) {
386 os << "for ";
387 base->print( os, indent + 2 );
388 } // if
389 break;
390 case EnumConstant:
391 os << "enumeration constant ";
392 break;
393 case Symbolic:
394 if ( symbolic.isTypedef ) {
395 os << "typedef definition ";
396 } else {
397 os << "type definition ";
398 } // if
399 if ( symbolic.params ) {
400 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
401 symbolic.params->printList( os, indent + 2 );
402 } // if
403 if ( symbolic.assertions ) {
404 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
405 symbolic.assertions->printList( os, indent + 4 );
406 os << string( indent + 2, ' ' );
407 } // if
408 if ( base ) {
409 os << "for ";
410 base->print( os, indent + 2 );
411 } // if
412 break;
413 case SymbolicInst:
414 os << *symbolic.name;
415 if ( symbolic.actuals ) {
416 os << "(";
417 symbolic.actuals->printList( os, indent + 2 );
418 os << ")";
419 } // if
420 break;
421 case Tuple:
422 os << "tuple ";
423 if ( tuple ) {
424 os << "with members" << endl;
425 tuple->printList( os, indent + 2 );
426 } // if
427 break;
428 case Basetypeof:
429 os << "base-";
430 #if defined(__GNUC__) && __GNUC__ >= 7
431 __attribute__((fallthrough));
432 #endif
433 // FALL THROUGH
434 case Typeof:
435 os << "type-of expression ";
436 if ( typeexpr ) {
437 typeexpr->print( os, indent + 2 );
438 } // if
439 break;
440 case Vtable:
441 os << "vtable";
442 break;
443 case Builtin:
444 os << DeclarationNode::builtinTypeNames[builtintype];
445 break;
446 case GlobalScope:
447 break;
448 case Qualified:
449 qualified.parent->print( os );
450 os << ".";
451 qualified.child->print( os );
452 break;
453 case Unknown:
454 os << "entity of unknown type ";
455 break;
456 default:
457 os << "internal error: TypeData::print " << kind << endl;
458 assert( false );
459 } // switch
460} // TypeData::print
461
462const std::string * TypeData::leafName() const {
463 switch ( kind ) {
464 case Unknown:
465 case Pointer:
466 case Reference:
467 case EnumConstant:
468 case GlobalScope:
469 case Array:
470 case Basic:
471 case Function:
472 case AggregateInst:
473 case Tuple:
474 case Typeof:
475 case Basetypeof:
476 case Builtin:
477 case Vtable:
478 assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
479 break;
480 case Aggregate:
481 return aggregate.name;
482 case Enum:
483 return enumeration.name;
484 case Symbolic:
485 case SymbolicInst:
486 return symbolic.name;
487 case Qualified:
488 return qualified.child->leafName();
489 } // switch
490 assert(false);
491}
492
493
494template< typename ForallList >
495void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
496 buildList( firstNode, outputList );
497 auto n = firstNode;
498 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
499 TypeDecl * td = static_cast<TypeDecl *>(*i);
500 if ( n->variable.tyClass == TypeDecl::Otype ) {
501 // add assertion parameters to `type' tyvars in reverse order
502 // add dtor: void ^?{}(T *)
503 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
504 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
505 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
506
507 // add copy ctor: void ?{}(T *, T)
508 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
509 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
510 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
511 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
512
513 // add default ctor: void ?{}(T *)
514 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
515 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
516 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
517
518 // add assignment operator: T * ?=?(T *, T)
519 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
520 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
521 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
522 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
523 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
524 } // if
525 } // for
526} // buildForall
527
528
529Type * typebuild( const TypeData * td ) {
530 assert( td );
531 switch ( td->kind ) {
532 case TypeData::Unknown:
533 // fill in implicit int
534 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
535 case TypeData::Basic:
536 return buildBasicType( td );
537 case TypeData::Pointer:
538 return buildPointer( td );
539 case TypeData::Array:
540 return buildArray( td );
541 case TypeData::Reference:
542 return buildReference( td );
543 case TypeData::Function:
544 return buildFunction( td );
545 case TypeData::AggregateInst:
546 return buildAggInst( td );
547 case TypeData::EnumConstant:
548 // the name gets filled in later -- by SymTab::Validate
549 return new EnumInstType( buildQualifiers( td ), "" );
550 case TypeData::SymbolicInst:
551 return buildSymbolicInst( td );
552 case TypeData::Tuple:
553 return buildTuple( td );
554 case TypeData::Typeof:
555 case TypeData::Basetypeof:
556 return buildTypeof( td );
557 case TypeData::Vtable:
558 return buildVtable( td );
559 case TypeData::Builtin:
560 switch ( td->builtintype ) {
561 case DeclarationNode::Zero:
562 return new ZeroType( noQualifiers );
563 case DeclarationNode::One:
564 return new OneType( noQualifiers );
565 default:
566 return new VarArgsType( buildQualifiers( td ) );
567 } // switch
568 case TypeData::GlobalScope:
569 return new GlobalScopeType();
570 case TypeData::Qualified:
571 return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
572 case TypeData::Symbolic:
573 case TypeData::Enum:
574 case TypeData::Aggregate:
575 assert( false );
576 } // switch
577
578 return nullptr;
579} // typebuild
580
581
582TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
583 TypeData * ret = nullptr;
584
585 switch ( td->kind ) {
586 case TypeData::Aggregate:
587 if ( ! toplevel && td->aggregate.body ) {
588 ret = td->clone();
589 } // if
590 break;
591 case TypeData::Enum:
592 if ( ! toplevel && td->enumeration.body ) {
593 ret = td->clone();
594 } // if
595 break;
596 case TypeData::AggregateInst:
597 if ( td->aggInst.aggregate ) {
598 ret = typeextractAggregate( td->aggInst.aggregate, false );
599 } // if
600 break;
601 default:
602 if ( td->base ) {
603 ret = typeextractAggregate( td->base, false );
604 } // if
605 } // switch
606 return ret;
607} // typeextractAggregate
608
609
610Type::Qualifiers buildQualifiers( const TypeData * td ) {
611 return td->qualifiers;
612} // buildQualifiers
613
614
615static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
616 SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
617} // genTSError
618
619Type * buildBasicType( const TypeData * td ) {
620 BasicType::Kind ret;
621
622 switch ( td->basictype ) {
623 case DeclarationNode::Void:
624 if ( td->signedness != DeclarationNode::NoSignedness ) {
625 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
626 } // if
627 if ( td->length != DeclarationNode::NoLength ) {
628 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
629 } // if
630 return new VoidType( buildQualifiers( td ) );
631 break;
632
633 case DeclarationNode::Bool:
634 if ( td->signedness != DeclarationNode::NoSignedness ) {
635 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
636 } // if
637 if ( td->length != DeclarationNode::NoLength ) {
638 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
639 } // if
640
641 ret = BasicType::Bool;
642 break;
643
644 case DeclarationNode::Char:
645 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
646 // character types. The implementation shall define char to have the same range, representation, and behavior as
647 // either signed char or unsigned char.
648 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
649
650 if ( td->length != DeclarationNode::NoLength ) {
651 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
652 } // if
653
654 ret = chartype[ td->signedness ];
655 break;
656
657 case DeclarationNode::Int:
658 static BasicType::Kind inttype[2][4] = {
659 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
660 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
661 };
662
663 Integral: ;
664 if ( td->signedness == DeclarationNode::NoSignedness ) {
665 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
666 } // if
667 ret = inttype[ td->signedness ][ td->length ];
668 break;
669
670 case DeclarationNode::Int128:
671 ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
672 if ( td->length != DeclarationNode::NoLength ) {
673 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
674 } // if
675 break;
676
677 case DeclarationNode::Float:
678 case DeclarationNode::Double:
679 case DeclarationNode::LongDouble: // not set until below
680 case DeclarationNode::uuFloat80:
681 case DeclarationNode::uuFloat128:
682 case DeclarationNode::uFloat16:
683 case DeclarationNode::uFloat32:
684 case DeclarationNode::uFloat32x:
685 case DeclarationNode::uFloat64:
686 case DeclarationNode::uFloat64x:
687 case DeclarationNode::uFloat128:
688 case DeclarationNode::uFloat128x:
689 static BasicType::Kind floattype[2][12] = {
690 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex, },
691 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, },
692 };
693
694 FloatingPoint: ;
695 if ( td->signedness != DeclarationNode::NoSignedness ) {
696 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
697 } // if
698 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
699 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
700 } // if
701 if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
702 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
703 } // if
704 if ( td->complextype == DeclarationNode::Imaginary ) {
705 genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
706 } // if
707 if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
708 genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
709 } // if
710 if ( td->length == DeclarationNode::Long ) {
711 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
712 } // if
713
714 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
715 //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
716 break;
717
718 case DeclarationNode::NoBasicType:
719 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
720 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
721 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
722 goto FloatingPoint;
723 } // if
724
725 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
726 goto Integral;
727 default:
728 assertf( false, "unknown basic type" );
729 return nullptr;
730 } // switch
731
732 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
733 buildForall( td->forall, bt->get_forall() );
734 return bt;
735} // buildBasicType
736
737
738PointerType * buildPointer( const TypeData * td ) {
739 PointerType * pt;
740 if ( td->base ) {
741 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
742 } else {
743 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
744 } // if
745 buildForall( td->forall, pt->get_forall() );
746 return pt;
747} // buildPointer
748
749
750ArrayType * buildArray( const TypeData * td ) {
751 ArrayType * at;
752 if ( td->base ) {
753 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
754 td->array.isVarLen, td->array.isStatic );
755 } else {
756 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
757 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
758 } // if
759 buildForall( td->forall, at->get_forall() );
760 return at;
761} // buildArray
762
763
764ReferenceType * buildReference( const TypeData * td ) {
765 ReferenceType * rt;
766 if ( td->base ) {
767 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
768 } else {
769 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
770 } // if
771 buildForall( td->forall, rt->get_forall() );
772 return rt;
773} // buildReference
774
775
776AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
777 assert( td->kind == TypeData::Aggregate );
778 AggregateDecl * at;
779 switch ( td->aggregate.kind ) {
780 case AggregateDecl::Struct:
781 case AggregateDecl::Coroutine:
782 case AggregateDecl::Exception:
783 case AggregateDecl::Generator:
784 case AggregateDecl::Monitor:
785 case AggregateDecl::Thread:
786 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
787 buildForall( td->aggregate.params, at->get_parameters() );
788 break;
789 case AggregateDecl::Union:
790 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
791 buildForall( td->aggregate.params, at->get_parameters() );
792 break;
793 case AggregateDecl::Trait:
794 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
795 buildList( td->aggregate.params, at->get_parameters() );
796 break;
797 default:
798 assert( false );
799 } // switch
800
801 buildList( td->aggregate.fields, at->get_members() );
802 at->set_body( td->aggregate.body );
803
804 return at;
805} // buildAggregate
806
807
808ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
809 switch ( type->kind ) {
810 case TypeData::Enum: {
811 if ( type->enumeration.body ) {
812 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
813 return new EnumInstType( buildQualifiers( type ), typedecl );
814 } else {
815 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
816 } // if
817 }
818 case TypeData::Aggregate: {
819 ReferenceToType * ret;
820 if ( type->aggregate.body ) {
821 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
822 switch ( type->aggregate.kind ) {
823 case AggregateDecl::Struct:
824 case AggregateDecl::Coroutine:
825 case AggregateDecl::Monitor:
826 case AggregateDecl::Thread:
827 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
828 break;
829 case AggregateDecl::Union:
830 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
831 break;
832 case AggregateDecl::Trait:
833 assert( false );
834 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
835 break;
836 default:
837 assert( false );
838 } // switch
839 } else {
840 switch ( type->aggregate.kind ) {
841 case AggregateDecl::Struct:
842 case AggregateDecl::Coroutine:
843 case AggregateDecl::Monitor:
844 case AggregateDecl::Thread:
845 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
846 break;
847 case AggregateDecl::Union:
848 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
849 break;
850 case AggregateDecl::Trait:
851 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
852 break;
853 default:
854 assert( false );
855 } // switch
856 } // if
857 return ret;
858 }
859 default:
860 assert( false );
861 } // switch
862} // buildAggInst
863
864
865ReferenceToType * buildAggInst( const TypeData * td ) {
866 assert( td->kind == TypeData::AggregateInst );
867
868 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
869 ReferenceToType * ret = nullptr;
870 TypeData * type = td->aggInst.aggregate;
871 switch ( type->kind ) {
872 case TypeData::Enum: {
873 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
874 }
875 case TypeData::Aggregate: {
876 switch ( type->aggregate.kind ) {
877 case AggregateDecl::Struct:
878 case AggregateDecl::Coroutine:
879 case AggregateDecl::Monitor:
880 case AggregateDecl::Thread:
881 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
882 break;
883 case AggregateDecl::Union:
884 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
885 break;
886 case AggregateDecl::Trait:
887 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
888 break;
889 default:
890 assert( false );
891 } // switch
892 }
893 break;
894 default:
895 assert( false );
896 } // switch
897
898 ret->set_hoistType( td->aggInst.hoistType );
899 buildList( td->aggInst.params, ret->get_parameters() );
900 buildForall( td->forall, ret->get_forall() );
901 return ret;
902} // buildAggInst
903
904
905NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
906 assert( td->kind == TypeData::Symbolic );
907 NamedTypeDecl * ret;
908 assert( td->base );
909 if ( td->symbolic.isTypedef ) {
910 ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
911 } else {
912 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
913 } // if
914 buildList( td->symbolic.assertions, ret->get_assertions() );
915 ret->base->attributes.splice( ret->base->attributes.end(), attributes );
916 return ret;
917} // buildSymbolic
918
919
920EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
921 assert( td->kind == TypeData::Enum );
922 Type * baseType = td->base ? typebuild(td->base) : nullptr;
923 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage, baseType );
924 buildList( td->enumeration.constants, ret->get_members() );
925 list< Declaration * >::iterator members = ret->get_members().begin();
926 for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
927 if ( cur->has_enumeratorValue() ) {
928 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
929 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
930 } else if ( !cur->initializer ) {
931 if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
932 SemanticError( td->location, "A non whole number enum value decl must be explicitly initialized." );
933 }
934 }
935 // else cur is a List Initializer and has been set as init in buildList()
936 // if
937 } // for
938 ret->set_body( td->enumeration.body );
939 return ret;
940} // buildEnum
941
942
943TypeInstType * buildSymbolicInst( const TypeData * td ) {
944 assert( td->kind == TypeData::SymbolicInst );
945 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
946 buildList( td->symbolic.actuals, ret->get_parameters() );
947 buildForall( td->forall, ret->get_forall() );
948 return ret;
949} // buildSymbolicInst
950
951
952TupleType * buildTuple( const TypeData * td ) {
953 assert( td->kind == TypeData::Tuple );
954 std::list< Type * > types;
955 buildTypeList( td->tuple, types );
956 TupleType * ret = new TupleType( buildQualifiers( td ), types );
957 buildForall( td->forall, ret->get_forall() );
958 return ret;
959} // buildTuple
960
961
962TypeofType * buildTypeof( const TypeData * td ) {
963 assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
964 assert( td->typeexpr );
965 // assert( td->typeexpr->expr );
966 return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
967} // buildTypeof
968
969
970VTableType * buildVtable( const TypeData * td ) {
971 assert( td->base );
972 return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
973} // buildVtable
974
975
976Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) {
977 if ( td->kind == TypeData::Function ) {
978 if ( td->function.idList ) { // KR function ?
979 buildKRFunction( td->function ); // transform into C11 function
980 } // if
981
982 FunctionDecl * decl;
983 Statement * stmt = maybeBuild<Statement>( td->function.body );
984 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
985 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
986 buildList( td->function.withExprs, decl->withExprs );
987 return decl->set_asmName( asmName );
988 } else if ( td->kind == TypeData::Aggregate ) {
989 return buildAggregate( td, attributes, linkage );
990 } else if ( td->kind == TypeData::Enum ) {
991 return buildEnum( td, attributes, linkage );
992 } else if ( td->kind == TypeData::Symbolic ) {
993 return buildSymbolic( td, attributes, name, scs, linkage );
994 } else {
995 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
996 } // if
997 return nullptr;
998} // buildDecl
999
1000
1001FunctionType * buildFunction( const TypeData * td ) {
1002 assert( td->kind == TypeData::Function );
1003 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
1004 buildList( td->function.params, ft->parameters );
1005 buildForall( td->forall, ft->forall );
1006 if ( td->base ) {
1007 switch ( td->base->kind ) {
1008 case TypeData::Tuple:
1009 buildList( td->base->tuple, ft->returnVals );
1010 break;
1011 default:
1012 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
1013 } // switch
1014 } else {
1015 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
1016 } // if
1017 return ft;
1018} // buildFunction
1019
1020
1021// Transform KR routine declarations into C99 routine declarations:
1022//
1023// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
1024//
1025// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
1026// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
1027// extra names are disallowed.
1028//
1029// Note, there is no KR routine-prototype syntax:
1030//
1031// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
1032// rtn(); // valid KR prototype
1033
1034void buildKRFunction( const TypeData::Function_t & function ) {
1035 assert( ! function.params );
1036 // loop over declaration first as it is easier to spot errors
1037 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
1038 // scan ALL parameter names for each declaration name to check for duplicates
1039 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1040 if ( *decl->name == *param->name ) {
1041 // type set => parameter name already transformed by a declaration names so there is a duplicate
1042 // declaration name attempting a second transformation
1043 if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
1044 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1045 // parameter name attempting a second transformation
1046 if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
1047 param->type = decl->type; // set copy declaration type to parameter type
1048 decl->type = nullptr; // reset declaration type
1049 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
1050 } // if
1051 } // for
1052 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
1053 if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
1054 } // for
1055
1056 // Parameter names without a declaration default to type int:
1057 //
1058 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
1059
1060 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1061 if ( ! param->type ) { // generate type int for empty parameter type
1062 param->type = new TypeData( TypeData::Basic );
1063 param->type->basictype = DeclarationNode::Int;
1064 } // if
1065 } // for
1066
1067 function.params = function.idList; // newly modified idList becomes parameters
1068 function.idList = nullptr; // idList now empty
1069 delete function.oldDeclList; // deletes entire list
1070 function.oldDeclList = nullptr; // reset
1071} // buildKRFunction
1072
1073// Local Variables: //
1074// tab-width: 4 //
1075// mode: c++ //
1076// compile-command: "make install" //
1077// End: //
Note: See TracBrowser for help on using the repository browser.