source: src/Parser/TypeData.cc @ 679a260

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

extend plan 9, anonymous declarations, change token for default argument

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