source: src/Parser/TypeData.cc@ 8477fc4

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 8477fc4 was 9f77301, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

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// 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 : Wed Jul 14 18:57:31 2021
13// Update Count : 672
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 ) { // basic type
286 assert( basictype != DeclarationNode::NoBasicType );
287 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
288 } else { // complex type
289 // handle double _Complex
290 if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
291 os << DeclarationNode::complexTypeNames[ complextype ] << " ";
292 } // if
293 break;
294 case Pointer:
295 os << "pointer ";
296 if ( base ) {
297 os << "to ";
298 base->print( os, indent );
299 } // if
300 break;
301 case Reference:
302 os << "reference ";
303 if ( base ) {
304 os << "to ";
305 base->print( os, indent );
306 } // if
307 break;
308 case Array:
309 if ( array.isStatic ) {
310 os << "static ";
311 } // if
312 if ( array.dimension ) {
313 os << "array of ";
314 array.dimension->printOneLine( os, indent );
315 } else if ( array.isVarLen ) {
316 os << "variable-length array of ";
317 } else {
318 os << "open array of ";
319 } // if
320 if ( base ) {
321 base->print( os, indent );
322 } // if
323 break;
324 case Function:
325 os << "function" << endl;
326 if ( function.params ) {
327 os << string( indent + 2, ' ' ) << "with parameters " << endl;
328 function.params->printList( os, indent + 4 );
329 } else {
330 os << string( indent + 2, ' ' ) << "with no parameters" << endl;
331 } // if
332 if ( function.idList ) {
333 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
334 function.idList->printList( os, indent + 4 );
335 } // if
336 if ( function.oldDeclList ) {
337 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
338 function.oldDeclList->printList( os, indent + 4 );
339 } // if
340 os << string( indent + 2, ' ' ) << "returning ";
341 if ( base ) {
342 base->print( os, indent + 4 );
343 } else {
344 os << "nothing ";
345 } // if
346 os << endl;
347 if ( function.body ) {
348 os << string( indent + 2, ' ' ) << "with body " << endl;
349 function.body->printList( os, indent + 2 );
350 } // if
351 break;
352 case Aggregate:
353 os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
354 if ( aggregate.params ) {
355 os << string( indent + 2, ' ' ) << "with type parameters" << endl;
356 aggregate.params->printList( os, indent + 4 );
357 } // if
358 if ( aggregate.actuals ) {
359 os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
360 aggregate.actuals->printList( os, indent + 4 );
361 } // if
362 if ( aggregate.fields ) {
363 os << string( indent + 2, ' ' ) << "with members" << endl;
364 aggregate.fields->printList( os, indent + 4 );
365 } // if
366 if ( aggregate.body ) {
367 os << string( indent + 2, ' ' ) << " with body" << endl;
368 } // if
369 break;
370 case AggregateInst:
371 if ( aggInst.aggregate ) {
372 os << "instance of " ;
373 aggInst.aggregate->print( os, indent );
374 } else {
375 os << "instance of an unspecified aggregate ";
376 } // if
377 if ( aggInst.params ) {
378 os << string( indent + 2, ' ' ) << "with parameters" << endl;
379 aggInst.params->printList( os, indent + 2 );
380 } // if
381 break;
382 case Enum:
383 os << "enumeration ";
384 if ( enumeration.constants ) {
385 os << "with constants" << endl;
386 enumeration.constants->printList( os, indent + 2 );
387 } // if
388 if ( enumeration.body ) {
389 os << string( indent + 2, ' ' ) << " with body" << endl;
390 } // if
391 break;
392 case EnumConstant:
393 os << "enumeration constant ";
394 break;
395 case Symbolic:
396 if ( symbolic.isTypedef ) {
397 os << "typedef definition ";
398 } else {
399 os << "type definition ";
400 } // if
401 if ( symbolic.params ) {
402 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
403 symbolic.params->printList( os, indent + 2 );
404 } // if
405 if ( symbolic.assertions ) {
406 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
407 symbolic.assertions->printList( os, indent + 4 );
408 os << string( indent + 2, ' ' );
409 } // if
410 if ( base ) {
411 os << "for ";
412 base->print( os, indent + 2 );
413 } // if
414 break;
415 case SymbolicInst:
416 os << *symbolic.name;
417 if ( symbolic.actuals ) {
418 os << "(";
419 symbolic.actuals->printList( os, indent + 2 );
420 os << ")";
421 } // if
422 break;
423 case Tuple:
424 os << "tuple ";
425 if ( tuple ) {
426 os << "with members" << endl;
427 tuple->printList( os, indent + 2 );
428 } // if
429 break;
430 case Basetypeof:
431 os << "base-";
432 #if defined(__GNUC__) && __GNUC__ >= 7
433 __attribute__((fallthrough));
434 #endif
435 case Typeof:
436 os << "type-of expression ";
437 if ( typeexpr ) {
438 typeexpr->print( os, indent + 2 );
439 } // if
440 break;
441 case Builtin:
442 os << DeclarationNode::builtinTypeNames[builtintype];
443 break;
444 case GlobalScope:
445 break;
446 case Qualified:
447 qualified.parent->print( os );
448 os << ".";
449 qualified.child->print( os );
450 break;
451 case Unknown:
452 os << "entity of unknown type ";
453 break;
454 default:
455 os << "internal error: TypeData::print " << kind << endl;
456 assert( false );
457 } // switch
458} // TypeData::print
459
460const std::string * TypeData::leafName() const {
461 switch ( kind ) {
462 case Unknown:
463 case Pointer:
464 case Reference:
465 case EnumConstant:
466 case GlobalScope:
467 case Array:
468 case Basic:
469 case Function:
470 case AggregateInst:
471 case Tuple:
472 case Typeof:
473 case Basetypeof:
474 case Builtin:
475 case Vtable:
476 assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
477 break;
478 case Aggregate:
479 return aggregate.name;
480 case Enum:
481 return enumeration.name;
482 case Symbolic:
483 case SymbolicInst:
484 return symbolic.name;
485 case Qualified:
486 return qualified.child->leafName();
487 } // switch
488 assert(false);
489}
490
491
492template< typename ForallList >
493void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
494 buildList( firstNode, outputList );
495 auto n = firstNode;
496 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
497 TypeDecl * td = static_cast<TypeDecl *>(*i);
498 if ( n->variable.tyClass == TypeDecl::Otype ) {
499 // add assertion parameters to `type' tyvars in reverse order
500 // add dtor: void ^?{}(T *)
501 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
502 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 ) );
503 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
504
505 // add copy ctor: void ?{}(T *, T)
506 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
507 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 ) );
508 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
509 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
510
511 // add default ctor: void ?{}(T *)
512 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
513 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 ) );
514 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
515
516 // add assignment operator: T * ?=?(T *, T)
517 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
518 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 ) );
519 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
520 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
521 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
522 } // if
523 } // for
524} // buildForall
525
526
527Type * typebuild( const TypeData * td ) {
528 assert( td );
529 switch ( td->kind ) {
530 case TypeData::Unknown:
531 // fill in implicit int
532 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
533 case TypeData::Basic:
534 return buildBasicType( td );
535 case TypeData::Pointer:
536 return buildPointer( td );
537 case TypeData::Array:
538 return buildArray( td );
539 case TypeData::Reference:
540 return buildReference( td );
541 case TypeData::Function:
542 return buildFunction( td );
543 case TypeData::AggregateInst:
544 return buildAggInst( td );
545 case TypeData::EnumConstant:
546 // the name gets filled in later -- by SymTab::Validate
547 return new EnumInstType( buildQualifiers( td ), "" );
548 case TypeData::SymbolicInst:
549 return buildSymbolicInst( td );
550 case TypeData::Tuple:
551 return buildTuple( td );
552 case TypeData::Typeof:
553 case TypeData::Basetypeof:
554 return buildTypeof( td );
555 case TypeData::Vtable:
556 return buildVtable( td );
557 case TypeData::Builtin:
558 switch ( td->builtintype ) {
559 case DeclarationNode::Zero:
560 return new ZeroType( noQualifiers );
561 case DeclarationNode::One:
562 return new OneType( noQualifiers );
563 default:
564 return new VarArgsType( buildQualifiers( td ) );
565 } // switch
566 case TypeData::GlobalScope:
567 return new GlobalScopeType();
568 case TypeData::Qualified:
569 return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
570 case TypeData::Symbolic:
571 case TypeData::Enum:
572 case TypeData::Aggregate:
573 assert( false );
574 } // switch
575
576 return nullptr;
577} // typebuild
578
579
580TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
581 TypeData * ret = nullptr;
582
583 switch ( td->kind ) {
584 case TypeData::Aggregate:
585 if ( ! toplevel && td->aggregate.body ) {
586 ret = td->clone();
587 } // if
588 break;
589 case TypeData::Enum:
590 if ( ! toplevel && td->enumeration.body ) {
591 ret = td->clone();
592 } // if
593 break;
594 case TypeData::AggregateInst:
595 if ( td->aggInst.aggregate ) {
596 ret = typeextractAggregate( td->aggInst.aggregate, false );
597 } // if
598 break;
599 default:
600 if ( td->base ) {
601 ret = typeextractAggregate( td->base, false );
602 } // if
603 } // switch
604 return ret;
605} // typeextractAggregate
606
607
608Type::Qualifiers buildQualifiers( const TypeData * td ) {
609 return td->qualifiers;
610} // buildQualifiers
611
612
613static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
614 SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
615} // genTSError
616
617Type * buildBasicType( const TypeData * td ) {
618 BasicType::Kind ret;
619
620 switch ( td->basictype ) {
621 case DeclarationNode::Void:
622 if ( td->signedness != DeclarationNode::NoSignedness ) {
623 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
624 } // if
625 if ( td->length != DeclarationNode::NoLength ) {
626 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
627 } // if
628 return new VoidType( buildQualifiers( td ) );
629 break;
630
631 case DeclarationNode::Bool:
632 if ( td->signedness != DeclarationNode::NoSignedness ) {
633 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
634 } // if
635 if ( td->length != DeclarationNode::NoLength ) {
636 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
637 } // if
638
639 ret = BasicType::Bool;
640 break;
641
642 case DeclarationNode::Char:
643 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
644 // character types. The implementation shall define char to have the same range, representation, and behavior as
645 // either signed char or unsigned char.
646 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
647
648 if ( td->length != DeclarationNode::NoLength ) {
649 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
650 } // if
651
652 ret = chartype[ td->signedness ];
653 break;
654
655 case DeclarationNode::Int:
656 static BasicType::Kind inttype[2][4] = {
657 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
658 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
659 };
660
661 Integral: ;
662 if ( td->signedness == DeclarationNode::NoSignedness ) {
663 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
664 } // if
665 ret = inttype[ td->signedness ][ td->length ];
666 break;
667
668 case DeclarationNode::Int128:
669 ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
670 if ( td->length != DeclarationNode::NoLength ) {
671 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
672 } // if
673 break;
674
675 case DeclarationNode::Float:
676 case DeclarationNode::Double:
677 case DeclarationNode::LongDouble: // not set until below
678 case DeclarationNode::uuFloat80:
679 case DeclarationNode::uuFloat128:
680 case DeclarationNode::uFloat16:
681 case DeclarationNode::uFloat32:
682 case DeclarationNode::uFloat32x:
683 case DeclarationNode::uFloat64:
684 case DeclarationNode::uFloat64x:
685 case DeclarationNode::uFloat128:
686 case DeclarationNode::uFloat128x:
687 static BasicType::Kind floattype[2][12] = {
688 { 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, },
689 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, },
690 };
691
692 FloatingPoint: ;
693 if ( td->signedness != DeclarationNode::NoSignedness ) {
694 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
695 } // if
696 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
697 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
698 } // if
699 if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
700 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
701 } // if
702 if ( td->complextype == DeclarationNode::Imaginary ) {
703 genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
704 } // if
705 if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
706 genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
707 } // if
708 if ( td->length == DeclarationNode::Long ) {
709 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
710 } // if
711
712 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
713 //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
714 break;
715
716 case DeclarationNode::NoBasicType:
717 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
718 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
719 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
720 goto FloatingPoint;
721 } // if
722
723 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
724 goto Integral;
725 default:
726 assertf( false, "unknown basic type" );
727 return nullptr;
728 } // switch
729
730 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
731 buildForall( td->forall, bt->get_forall() );
732 return bt;
733} // buildBasicType
734
735
736PointerType * buildPointer( const TypeData * td ) {
737 PointerType * pt;
738 if ( td->base ) {
739 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
740 } else {
741 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
742 } // if
743 buildForall( td->forall, pt->get_forall() );
744 return pt;
745} // buildPointer
746
747
748ArrayType * buildArray( const TypeData * td ) {
749 ArrayType * at;
750 if ( td->base ) {
751 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
752 td->array.isVarLen, td->array.isStatic );
753 } else {
754 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
755 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
756 } // if
757 buildForall( td->forall, at->get_forall() );
758 return at;
759} // buildArray
760
761
762ReferenceType * buildReference( const TypeData * td ) {
763 ReferenceType * rt;
764 if ( td->base ) {
765 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
766 } else {
767 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
768 } // if
769 buildForall( td->forall, rt->get_forall() );
770 return rt;
771} // buildReference
772
773
774AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
775 assert( td->kind == TypeData::Aggregate );
776 AggregateDecl * at;
777 switch ( td->aggregate.kind ) {
778 case AggregateDecl::Struct:
779 case AggregateDecl::Coroutine:
780 case AggregateDecl::Generator:
781 case AggregateDecl::Monitor:
782 case AggregateDecl::Thread:
783 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
784 buildForall( td->aggregate.params, at->get_parameters() );
785 break;
786 case AggregateDecl::Union:
787 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
788 buildForall( td->aggregate.params, at->get_parameters() );
789 break;
790 case AggregateDecl::Trait:
791 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
792 buildList( td->aggregate.params, at->get_parameters() );
793 break;
794 default:
795 assert( false );
796 } // switch
797
798 buildList( td->aggregate.fields, at->get_members() );
799 at->set_body( td->aggregate.body );
800
801 return at;
802} // buildAggregate
803
804
805ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
806 switch ( type->kind ) {
807 case TypeData::Enum: {
808 if ( type->enumeration.body ) {
809 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
810 return new EnumInstType( buildQualifiers( type ), typedecl );
811 } else {
812 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
813 } // if
814 }
815 case TypeData::Aggregate: {
816 ReferenceToType * ret;
817 if ( type->aggregate.body ) {
818 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
819 switch ( type->aggregate.kind ) {
820 case AggregateDecl::Struct:
821 case AggregateDecl::Coroutine:
822 case AggregateDecl::Monitor:
823 case AggregateDecl::Thread:
824 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
825 break;
826 case AggregateDecl::Union:
827 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
828 break;
829 case AggregateDecl::Trait:
830 assert( false );
831 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
832 break;
833 default:
834 assert( false );
835 } // switch
836 } else {
837 switch ( type->aggregate.kind ) {
838 case AggregateDecl::Struct:
839 case AggregateDecl::Coroutine:
840 case AggregateDecl::Monitor:
841 case AggregateDecl::Thread:
842 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
843 break;
844 case AggregateDecl::Union:
845 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
846 break;
847 case AggregateDecl::Trait:
848 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
849 break;
850 default:
851 assert( false );
852 } // switch
853 } // if
854 return ret;
855 }
856 default:
857 assert( false );
858 } // switch
859} // buildAggInst
860
861
862ReferenceToType * buildAggInst( const TypeData * td ) {
863 assert( td->kind == TypeData::AggregateInst );
864
865 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
866 ReferenceToType * ret = nullptr;
867 TypeData * type = td->aggInst.aggregate;
868 switch ( type->kind ) {
869 case TypeData::Enum: {
870 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
871 }
872 case TypeData::Aggregate: {
873 switch ( type->aggregate.kind ) {
874 case AggregateDecl::Struct:
875 case AggregateDecl::Coroutine:
876 case AggregateDecl::Monitor:
877 case AggregateDecl::Thread:
878 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
879 break;
880 case AggregateDecl::Union:
881 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
882 break;
883 case AggregateDecl::Trait:
884 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
885 break;
886 default:
887 assert( false );
888 } // switch
889 }
890 break;
891 default:
892 assert( false );
893 } // switch
894
895 ret->set_hoistType( td->aggInst.hoistType );
896 buildList( td->aggInst.params, ret->get_parameters() );
897 buildForall( td->forall, ret->get_forall() );
898 return ret;
899} // buildAggInst
900
901
902NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
903 assert( td->kind == TypeData::Symbolic );
904 NamedTypeDecl * ret;
905 assert( td->base );
906 if ( td->symbolic.isTypedef ) {
907 ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
908 } else {
909 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
910 } // if
911 buildList( td->symbolic.assertions, ret->get_assertions() );
912 ret->base->attributes.splice( ret->base->attributes.end(), attributes );
913 return ret;
914} // buildSymbolic
915
916
917EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
918 assert( td->kind == TypeData::Enum );
919 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
920 buildList( td->enumeration.constants, ret->get_members() );
921 list< Declaration * >::iterator members = ret->get_members().begin();
922 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
923 if ( cur->has_enumeratorValue() ) {
924 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
925 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
926 } // if
927 } // for
928 ret->set_body( td->enumeration.body );
929 return ret;
930} // buildEnum
931
932
933TypeInstType * buildSymbolicInst( const TypeData * td ) {
934 assert( td->kind == TypeData::SymbolicInst );
935 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
936 buildList( td->symbolic.actuals, ret->get_parameters() );
937 buildForall( td->forall, ret->get_forall() );
938 return ret;
939} // buildSymbolicInst
940
941
942TupleType * buildTuple( const TypeData * td ) {
943 assert( td->kind == TypeData::Tuple );
944 std::list< Type * > types;
945 buildTypeList( td->tuple, types );
946 TupleType * ret = new TupleType( buildQualifiers( td ), types );
947 buildForall( td->forall, ret->get_forall() );
948 return ret;
949} // buildTuple
950
951
952TypeofType * buildTypeof( const TypeData * td ) {
953 assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
954 assert( td->typeexpr );
955 // assert( td->typeexpr->expr );
956 return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
957} // buildTypeof
958
959
960VTableType * buildVtable( const TypeData * td ) {
961 assert( td->base );
962 return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
963} // buildVtable
964
965
966Declaration * 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 ) {
967 if ( td->kind == TypeData::Function ) {
968 if ( td->function.idList ) { // KR function ?
969 buildKRFunction( td->function ); // transform into C11 function
970 } // if
971
972 FunctionDecl * decl;
973 Statement * stmt = maybeBuild<Statement>( td->function.body );
974 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
975 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
976 buildList( td->function.withExprs, decl->withExprs );
977 return decl->set_asmName( asmName );
978 } else if ( td->kind == TypeData::Aggregate ) {
979 return buildAggregate( td, attributes, linkage );
980 } else if ( td->kind == TypeData::Enum ) {
981 return buildEnum( td, attributes, linkage );
982 } else if ( td->kind == TypeData::Symbolic ) {
983 return buildSymbolic( td, attributes, name, scs, linkage );
984 } else {
985 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
986 } // if
987 return nullptr;
988} // buildDecl
989
990
991FunctionType * buildFunction( const TypeData * td ) {
992 assert( td->kind == TypeData::Function );
993 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
994 buildList( td->function.params, ft->parameters );
995 buildForall( td->forall, ft->forall );
996 if ( td->base ) {
997 switch ( td->base->kind ) {
998 case TypeData::Tuple:
999 buildList( td->base->tuple, ft->returnVals );
1000 break;
1001 default:
1002 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
1003 } // switch
1004 } else {
1005 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
1006 } // if
1007 return ft;
1008} // buildFunction
1009
1010
1011// Transform KR routine declarations into C99 routine declarations:
1012//
1013// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
1014//
1015// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
1016// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
1017// extra names are disallowed.
1018//
1019// Note, there is no KR routine-prototype syntax:
1020//
1021// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
1022// rtn(); // valid KR prototype
1023
1024void buildKRFunction( const TypeData::Function_t & function ) {
1025 assert( ! function.params );
1026 // loop over declaration first as it is easier to spot errors
1027 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
1028 // scan ALL parameter names for each declaration name to check for duplicates
1029 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1030 if ( *decl->name == *param->name ) {
1031 // type set => parameter name already transformed by a declaration names so there is a duplicate
1032 // declaration name attempting a second transformation
1033 if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
1034 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1035 // parameter name attempting a second transformation
1036 if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
1037 param->type = decl->type; // set copy declaration type to parameter type
1038 decl->type = nullptr; // reset declaration type
1039 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
1040 } // if
1041 } // for
1042 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
1043 if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
1044 } // for
1045
1046 // Parameter names without a declaration default to type int:
1047 //
1048 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
1049
1050 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1051 if ( ! param->type ) { // generate type int for empty parameter type
1052 param->type = new TypeData( TypeData::Basic );
1053 param->type->basictype = DeclarationNode::Int;
1054 } // if
1055 } // for
1056
1057 function.params = function.idList; // newly modified idList becomes parameters
1058 function.idList = nullptr; // idList now empty
1059 delete function.oldDeclList; // deletes entire list
1060 function.oldDeclList = nullptr; // reset
1061} // buildKRFunction
1062
1063// Local Variables: //
1064// tab-width: 4 //
1065// mode: c++ //
1066// compile-command: "make install" //
1067// End: //
Note: See TracBrowser for help on using the repository browser.