source: src/Parser/TypeData.cc @ 8a1d95af

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since 8a1d95af was 0c730d9, checked in by Henry Xue <y58xue@…>, 3 years ago

Translate exception declarations

  • 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 : Henry Xue
12// Last Modified On : Tue Jul 20 04:10:50 2021
13// Update Count     : 673
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::Exception:
781          case AggregateDecl::Generator:
782          case AggregateDecl::Monitor:
783          case AggregateDecl::Thread:
784                at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
785                buildForall( td->aggregate.params, at->get_parameters() );
786                break;
787          case AggregateDecl::Union:
788                at = new UnionDecl( *td->aggregate.name, attributes, linkage );
789                buildForall( td->aggregate.params, at->get_parameters() );
790                break;
791          case AggregateDecl::Trait:
792                at = new TraitDecl( *td->aggregate.name, attributes, linkage );
793                buildList( td->aggregate.params, at->get_parameters() );
794                break;
795          default:
796                assert( false );
797        } // switch
798
799        buildList( td->aggregate.fields, at->get_members() );
800        at->set_body( td->aggregate.body );
801
802        return at;
803} // buildAggregate
804
805
806ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
807        switch ( type->kind ) {
808          case TypeData::Enum: {
809                  if ( type->enumeration.body ) {
810                          EnumDecl * typedecl = buildEnum( type, attributes, linkage );
811                          return new EnumInstType( buildQualifiers( type ), typedecl );
812                  } else {
813                          return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
814                  } // if
815          }
816          case TypeData::Aggregate: {
817                  ReferenceToType * ret;
818                  if ( type->aggregate.body ) {
819                          AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
820                          switch ( type->aggregate.kind ) {
821                                case AggregateDecl::Struct:
822                                case AggregateDecl::Coroutine:
823                                case AggregateDecl::Monitor:
824                                case AggregateDecl::Thread:
825                                  ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
826                                  break;
827                                case AggregateDecl::Union:
828                                  ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
829                                  break;
830                                case AggregateDecl::Trait:
831                                  assert( false );
832                                  //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
833                                  break;
834                                default:
835                                  assert( false );
836                          } // switch
837                  } else {
838                          switch ( type->aggregate.kind ) {
839                                case AggregateDecl::Struct:
840                                case AggregateDecl::Coroutine:
841                                case AggregateDecl::Monitor:
842                                case AggregateDecl::Thread:
843                                  ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
844                                  break;
845                                case AggregateDecl::Union:
846                                  ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
847                                  break;
848                                case AggregateDecl::Trait:
849                                  ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
850                                  break;
851                                default:
852                                  assert( false );
853                          } // switch
854                  } // if
855                  return ret;
856          }
857          default:
858                assert( false );
859        } // switch
860} // buildAggInst
861
862
863ReferenceToType * buildAggInst( const TypeData * td ) {
864        assert( td->kind == TypeData::AggregateInst );
865
866        // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
867        ReferenceToType * ret = nullptr;
868        TypeData * type = td->aggInst.aggregate;
869        switch ( type->kind ) {
870          case TypeData::Enum: {
871                  return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
872          }
873          case TypeData::Aggregate: {
874                  switch ( type->aggregate.kind ) {
875                        case AggregateDecl::Struct:
876                        case AggregateDecl::Coroutine:
877                        case AggregateDecl::Monitor:
878                        case AggregateDecl::Thread:
879                          ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
880                          break;
881                        case AggregateDecl::Union:
882                          ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
883                          break;
884                        case AggregateDecl::Trait:
885                          ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
886                          break;
887                        default:
888                          assert( false );
889                  } // switch
890          }
891          break;
892          default:
893                assert( false );
894        } // switch
895
896        ret->set_hoistType( td->aggInst.hoistType );
897        buildList( td->aggInst.params, ret->get_parameters() );
898        buildForall( td->forall, ret->get_forall() );
899        return ret;
900} // buildAggInst
901
902
903NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
904        assert( td->kind == TypeData::Symbolic );
905        NamedTypeDecl * ret;
906        assert( td->base );
907        if ( td->symbolic.isTypedef ) {
908                ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
909        } else {
910                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
911        } // if
912        buildList( td->symbolic.assertions, ret->get_assertions() );
913        ret->base->attributes.splice( ret->base->attributes.end(), attributes );
914        return ret;
915} // buildSymbolic
916
917
918EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
919        assert( td->kind == TypeData::Enum );
920        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
921        buildList( td->enumeration.constants, ret->get_members() );
922        list< Declaration * >::iterator members = ret->get_members().begin();
923        for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
924                if ( cur->has_enumeratorValue() ) {
925                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
926                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
927                } // if
928        } // for
929        ret->set_body( td->enumeration.body );
930        return ret;
931} // buildEnum
932
933
934TypeInstType * buildSymbolicInst( const TypeData * td ) {
935        assert( td->kind == TypeData::SymbolicInst );
936        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
937        buildList( td->symbolic.actuals, ret->get_parameters() );
938        buildForall( td->forall, ret->get_forall() );
939        return ret;
940} // buildSymbolicInst
941
942
943TupleType * buildTuple( const TypeData * td ) {
944        assert( td->kind == TypeData::Tuple );
945        std::list< Type * > types;
946        buildTypeList( td->tuple, types );
947        TupleType * ret = new TupleType( buildQualifiers( td ), types );
948        buildForall( td->forall, ret->get_forall() );
949        return ret;
950} // buildTuple
951
952
953TypeofType * buildTypeof( const TypeData * td ) {
954        assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
955        assert( td->typeexpr );
956        // assert( td->typeexpr->expr );
957        return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
958} // buildTypeof
959
960
961VTableType * buildVtable( const TypeData * td ) {
962        assert( td->base );
963        return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
964} // buildVtable
965
966
967Declaration * 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 ) {
968        if ( td->kind == TypeData::Function ) {
969                if ( td->function.idList ) {                                    // KR function ?
970                        buildKRFunction( td->function );                        // transform into C11 function
971                } // if
972
973                FunctionDecl * decl;
974                Statement * stmt = maybeBuild<Statement>( td->function.body );
975                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
976                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
977                buildList( td->function.withExprs, decl->withExprs );
978                return decl->set_asmName( asmName );
979        } else if ( td->kind == TypeData::Aggregate ) {
980                return buildAggregate( td, attributes, linkage );
981        } else if ( td->kind == TypeData::Enum ) {
982                return buildEnum( td, attributes, linkage );
983        } else if ( td->kind == TypeData::Symbolic ) {
984                return buildSymbolic( td, attributes, name, scs, linkage );
985        } else {
986                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
987        } // if
988        return nullptr;
989} // buildDecl
990
991
992FunctionType * buildFunction( const TypeData * td ) {
993        assert( td->kind == TypeData::Function );
994        FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
995        buildList( td->function.params, ft->parameters );
996        buildForall( td->forall, ft->forall );
997        if ( td->base ) {
998                switch ( td->base->kind ) {
999                  case TypeData::Tuple:
1000                        buildList( td->base->tuple, ft->returnVals );
1001                        break;
1002                  default:
1003                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
1004                } // switch
1005        } else {
1006                ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
1007        } // if
1008        return ft;
1009} // buildFunction
1010
1011
1012// Transform KR routine declarations into C99 routine declarations:
1013//
1014//    rtn( a, b, c ) int a, c; double b {}  =>  int rtn( int a, double c, int b ) {}
1015//
1016// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
1017// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
1018// extra names are disallowed.
1019//
1020// Note, there is no KR routine-prototype syntax:
1021//
1022//    rtn( a, b, c ) int a, c; double b; // invalid KR prototype
1023//    rtn(); // valid KR prototype
1024
1025void buildKRFunction( const TypeData::Function_t & function ) {
1026        assert( ! function.params );
1027        // loop over declaration first as it is easier to spot errors
1028        for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
1029                // scan ALL parameter names for each declaration name to check for duplicates
1030                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1031                        if ( *decl->name == *param->name ) {
1032                                // type set => parameter name already transformed by a declaration names so there is a duplicate
1033                                // declaration name attempting a second transformation
1034                                if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
1035                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1036                                // parameter name attempting a second transformation
1037                                if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
1038                                param->type = decl->type;                               // set copy declaration type to parameter type
1039                                decl->type = nullptr;                                   // reset declaration type
1040                                param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
1041                        } // if
1042                } // for
1043                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
1044                if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
1045        } // for
1046
1047        // Parameter names without a declaration default to type int:
1048        //
1049        //    rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
1050
1051        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1052                if ( ! param->type ) {                                                  // generate type int for empty parameter type
1053                        param->type = new TypeData( TypeData::Basic );
1054                        param->type->basictype = DeclarationNode::Int;
1055                } // if
1056        } // for
1057
1058        function.params = function.idList;                                      // newly modified idList becomes parameters
1059        function.idList = nullptr;                                                      // idList now empty
1060        delete function.oldDeclList;                                            // deletes entire list
1061        function.oldDeclList = nullptr;                                         // reset
1062} // buildKRFunction
1063
1064// Local Variables: //
1065// tab-width: 4 //
1066// mode: c++ //
1067// compile-command: "make install" //
1068// End: //
Note: See TracBrowser for help on using the repository browser.