source: src/Parser/TypeData.cc @ 5695645

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 5695645 was 9e7236f4, checked in by JiadaL <j82liang@…>, 2 years ago

Resolution of struct enum. The codegen of struct enum will be in the next commit

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