source: src/AST/Decl.cpp @ f5ec35a

Last change on this file since f5ec35a was 8941b6b, checked in by Andrew Beach <ajbeach@…>, 8 months ago

Direct translation of code generation.

  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[2bb4a01]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// Decl.cpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Thu May 9 10:00:00 2019
[7edd5c1]11// Last Modified By : Andrew Beach
12// Last Modified On : Thu May  5 12:10:00 2022
13// Update Count     : 24
[2bb4a01]14//
15
16#include "Decl.hpp"
17
[360b2e13]18#include <cassert>             // for assert, strict_dynamic_cast
[d76c588]19#include <iostream>
[360b2e13]20#include <unordered_map>
21
[8941b6b]22#include "CodeGen/FixMain.h"   // for FixMain
[8f06277]23#include "Common/Eval.h"       // for eval
[87701b6]24
[360b2e13]25#include "Fwd.hpp"             // for UniqueId
[a300e4a]26#include "Init.hpp"
[360b2e13]27#include "Node.hpp"            // for readonly
[87701b6]28#include "Type.hpp"            // for readonly
[a4a6802]29#include "Expr.hpp"
[2bb4a01]30
31namespace ast {
[a300e4a]32
[2bb4a01]33// To canonicalize declarations
34static UniqueId lastUniqueId = 0;
35
36using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
37static IdMapType idMap;
38
39void Decl::fixUniqueId() {
40        if ( uniqueId ) return;  // ensure only set once
41        uniqueId = ++lastUniqueId;
[7d7ef6f]42        // The extra readonly pointer is causing some reference counting issues.
43        // idMap[ uniqueId ] = this;
[2bb4a01]44}
45
46readonly<Decl> Decl::fromId( UniqueId id ) {
[7d7ef6f]47        // Right now this map is always empty, so don't use it.
[9490621]48        assert( false );
[2bb4a01]49        IdMapType::const_iterator i = idMap.find( id );
50        if ( i != idMap.end() ) return i->second;
51        return {};
52}
[a300e4a]53
[8a5530c]54// --- FunctionDecl
55
[7edd5c1]56FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name,
[3e5dd913]57        std::vector<ptr<TypeDecl>>&& forall,
58        std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
59        CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
[3e94a23]60        std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, ArgumentFlag isVarArgs )
[b859f59]61: DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ),
62        type_params(std::move(forall)), assertions(),
63        params(std::move(params)), returns(std::move(returns)), stmts( stmts ) {
[3e94a23]64        FunctionType * ftype = new FunctionType( isVarArgs );
[3e5dd913]65        for (auto & param : this->params) {
66                ftype->params.emplace_back(param->get_type());
67        }
68        for (auto & ret : this->returns) {
69                ftype->returns.emplace_back(ret->get_type());
70        }
71        for (auto & tp : this->type_params) {
[b230091]72                ftype->forall.emplace_back(new TypeInstType(tp));
[a4a6802]73                for (auto & ap: tp->assertions) {
74                        ftype->assertions.emplace_back(new VariableExpr(loc, ap));
75                }
[3e5dd913]76        }
77        this->type = ftype;
[8941b6b]78        // Hack forcing the function "main" to have Cforall linkage to replace
79        // main even if it is inside an extern "C", and also makes sure the
80        // replacing function is always a C function.
81        if ( name == "main" ) {
82                this->linkage = CodeGen::FixMain::getMainLinkage();
83        }
[3e5dd913]84}
[490fb92e]85
[7edd5c1]86FunctionDecl::FunctionDecl( const CodeLocation & location, const std::string & name,
87        std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
88        std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
89        CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
[3e94a23]90        std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, ArgumentFlag isVarArgs )
[7edd5c1]91: DeclWithType( location, name, storage, linkage, std::move(attrs), fs ),
92                type_params( std::move( forall) ), assertions( std::move( assertions ) ),
[b859f59]93                params( std::move(params) ), returns( std::move(returns) ),
[7edd5c1]94                type( nullptr ), stmts( stmts ) {
[3e94a23]95        FunctionType * type = new FunctionType( isVarArgs );
[7edd5c1]96        for ( auto & param : this->params ) {
97                type->params.emplace_back( param->get_type() );
98        }
99        for ( auto & ret : this->returns ) {
100                type->returns.emplace_back( ret->get_type() );
101        }
102        for ( auto & param : this->type_params ) {
103                type->forall.emplace_back( new TypeInstType( param ) );
104        }
105        for ( auto & assertion : this->assertions ) {
106                type->assertions.emplace_back(
107                        new VariableExpr( assertion->location, assertion ) );
108        }
109        this->type = type;
[8941b6b]110        // See note above about this hack.
111        if ( name == "main" ) {
112                this->linkage = CodeGen::FixMain::getMainLinkage();
113        }
[7edd5c1]114}
115
[490fb92e]116
[87701b6]117const Type * FunctionDecl::get_type() const { return type.get(); }
[e0e9a0b]118void FunctionDecl::set_type( const Type * t ) {
119        type = strict_dynamic_cast< const FunctionType * >( t );
120}
[8a5530c]121
[360b2e13]122// --- TypeDecl
123
[312029a]124const char * TypeDecl::typeString() const {
[6e50a6b]125        static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" };
[b66d14a]126        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
[07de76b]127        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
[97b47ec]128        // sizeof("sized") includes '\0' and gives the offset to remove "sized ".
129        return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ];
[360b2e13]130}
131
[312029a]132const char * TypeDecl::genTypeString() const {
[b66d14a]133        static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" };
134        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
[07de76b]135        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
[360b2e13]136        return kindNames[ kind ];
137}
138
[93c10de]139std::ostream & operator<< ( std::ostream & out, const TypeData & data ) {
[d76c588]140        return out << data.kind << ", " << data.isComplete;
141}
142
[312029a]143// --- AggregateDecl
144
145// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
146static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
147
148const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
149        return aggregateNames[aggr];
150}
151
[a300e4a]152// --- EnumDecl
153
[9d6e7fa9]154bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
[a300e4a]155        if ( enumValues.empty() ) {
[8f557161]156                Evaluation crntVal = {0, true, true};  // until expression is given, we know to start counting from 0
[9d6e7fa9]157                for ( const Decl * member : members ) {
[a300e4a]158                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
159                        if ( field->init ) {
[87701b6]160                                const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
[8f557161]161                                crntVal = eval( init->value );
162                                if ( ! crntVal.isEvaluableInGCC ) {
[733074e]163                                        SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
[a300e4a]164                                                "enumerator: ", field ) );
165                                }
166                        }
167                        if ( enumValues.count( field->name ) != 0 ) {
[733074e]168                                SemanticError( location, ::toString( "Enum ", name, " has multiple members with the "   "name ", field->name ) );
[a300e4a]169                        }
[8f557161]170                        if (crntVal.hasKnownValue) {
171                                enumValues[ field->name ] = crntVal.knownValue;
172                        }
173                        ++crntVal.knownValue;
[a300e4a]174                }
175        }
176
177        auto it = enumValues.find( enumerator->name );
[b859f59]178
[a300e4a]179        if ( it != enumValues.end() ) {
[b859f59]180
[f135b50]181                // Handle typed enum by casting the value in (C++) compiler
[3e54399]182                // if ( base ) { // A typed enum
183                //      if ( const BasicType * bt = dynamic_cast<const BasicType *>(base) ) {
184                //              switch( bt->kind ) {
185                //                      case BasicType::Kind::Bool:     value = (bool) it->second; break;
186                //                      case BasicType::Kind::Char: value = (char) it->second; break;
187                //                      case BasicType::Kind::SignedChar: value = (signed char) it->second; break;
188                //                      case BasicType::Kind::UnsignedChar: value = (unsigned char) it->second; break;
189                //                      case BasicType::Kind::ShortSignedInt: value = (short signed int) it->second; break;
190                //                      case BasicType::Kind::SignedInt: value = (signed int) it->second; break;
191                //                      case BasicType::Kind::UnsignedInt: value = (unsigned int) it->second; break;
192                //                      case BasicType::Kind::LongSignedInt: value = (long signed int) it->second; break;
193                //                      case BasicType::Kind::LongUnsignedInt: value = (long unsigned int) it->second; break;
194                //                      case BasicType::Kind::LongLongSignedInt: value = (long long signed int) it->second; break;
[b859f59]195                //                      case BasicType::Kind::LongLongUnsignedInt: value = (long long unsigned int) it->second; break;
[3e54399]196                //                      // TODO: value should be able to handle long long unsigned int
197
198                //                      default:
199                //                      value = it->second;
200                //              }
201                //      }
202                // } else {
[f135b50]203                        value = it->second;
[3e54399]204                //}
[f135b50]205
[a300e4a]206                return true;
207        }
208        return false;
209}
210
[2bb4a01]211}
212
213// Local Variables: //
214// tab-width: 4 //
215// mode: c++ //
216// compile-command: "make install" //
217// End: //
Note: See TracBrowser for help on using the repository browser.