source: src/Parser/TypeData.cc @ ad64520

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since ad64520 was 8f91c9ae, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add inline qualifier to aggregate fields to separate plan 9 and forward semantics

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