source: src/AST/Decl.cpp @ 1784e9e

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 1784e9e was 92538ab, checked in by JiadaL <j82liang@…>, 2 years ago

Resolve conflict

  • Property mode set to 100644
File size: 6.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// Decl.cpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Thu May 9 10:00:00 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jan 12 16:54:55 2021
13// Update Count     : 23
14//
15
16#include "Decl.hpp"
17
18#include <cassert>             // for assert, strict_dynamic_cast
19#include <iostream>
20#include <unordered_map>
21
22#include "Common/utility.h"
23
24#include "Fwd.hpp"             // for UniqueId
25#include "Init.hpp"
26#include "Node.hpp"            // for readonly
27#include "Type.hpp"            // for readonly
28#include "Expr.hpp"
29
30namespace ast {
31
32// To canonicalize declarations
33static UniqueId lastUniqueId = 0;
34
35using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
36static IdMapType idMap;
37
38void Decl::fixUniqueId() {
39        if ( uniqueId ) return;  // ensure only set once
40        uniqueId = ++lastUniqueId;
41        // The extra readonly pointer is causing some reference counting issues.
42        // idMap[ uniqueId ] = this;
43}
44
45readonly<Decl> Decl::fromId( UniqueId id ) {
46        // Right now this map is always empty, so don't use it.
47        assert( false );
48        IdMapType::const_iterator i = idMap.find( id );
49        if ( i != idMap.end() ) return i->second;
50        return {};
51}
52
53// --- FunctionDecl
54
55FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name, 
56        std::vector<ptr<TypeDecl>>&& forall,
57        std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
58        CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
59        std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs)
60: DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
61        type_params(std::move(forall)), stmts( stmts ) {
62        FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs));
63        for (auto & param : this->params) {
64                ftype->params.emplace_back(param->get_type());
65        }
66        for (auto & ret : this->returns) {
67                ftype->returns.emplace_back(ret->get_type());
68        }
69        for (auto & tp : this->type_params) {
70                ftype->forall.emplace_back(new TypeInstType(tp));
71                for (auto & ap: tp->assertions) {
72                        ftype->assertions.emplace_back(new VariableExpr(loc, ap));
73                }
74        }
75        this->type = ftype;
76}
77
78
79const Type * FunctionDecl::get_type() const { return type.get(); }
80void FunctionDecl::set_type( const Type * t ) {
81        type = strict_dynamic_cast< const FunctionType * >( t );
82}
83
84// --- TypeDecl
85
86const char * TypeDecl::typeString() const {
87        static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" };
88        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
89        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
90        return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0'
91}
92
93const char * TypeDecl::genTypeString() const {
94        static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" };
95        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
96        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
97        return kindNames[ kind ];
98}
99
100std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
101        return out << data.kind << ", " << data.isComplete;
102}
103
104// --- AggregateDecl
105
106// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
107static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
108
109const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
110        return aggregateNames[aggr];
111}
112
113// --- EnumDecl
114
115bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
116        if ( enumValues.empty() ) {
117                long long crntVal = 0;
118                for ( const Decl * member : members ) {
119                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
120                        if ( field->init ) {
121                                const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
122                                auto result = eval( init->value );
123                                if ( ! result.second ) {
124                                        SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
125                                                "enumerator: ", field ) );
126                                }
127                                crntVal = result.first;
128                        }
129                        if ( enumValues.count( field->name ) != 0 ) {
130                                SemanticError( location, ::toString( "Enum ", name, " has multiple members with the "   "name ", field->name ) );
131                        }
132                        enumValues[ field->name ] = crntVal;
133                        ++crntVal;
134                }
135        }
136
137        auto it = enumValues.find( enumerator->name );
138       
139        if ( it != enumValues.end() ) {
140                       
141                // Handle typed enum by casting the value in (C++) compiler
142                // if ( base ) { // A typed enum
143                //      if ( const BasicType * bt = dynamic_cast<const BasicType *>(base) ) {
144                //              switch( bt->kind ) {
145                //                      case BasicType::Kind::Bool:     value = (bool) it->second; break;
146                //                      case BasicType::Kind::Char: value = (char) it->second; break;
147                //                      case BasicType::Kind::SignedChar: value = (signed char) it->second; break;
148                //                      case BasicType::Kind::UnsignedChar: value = (unsigned char) it->second; break;
149                //                      case BasicType::Kind::ShortSignedInt: value = (short signed int) it->second; break;
150                //                      case BasicType::Kind::SignedInt: value = (signed int) it->second; break;
151                //                      case BasicType::Kind::UnsignedInt: value = (unsigned int) it->second; break;
152                //                      case BasicType::Kind::LongSignedInt: value = (long signed int) it->second; break;
153                //                      case BasicType::Kind::LongUnsignedInt: value = (long unsigned int) it->second; break;
154                //                      case BasicType::Kind::LongLongSignedInt: value = (long long signed int) it->second; break;
155                //                      case BasicType::Kind::LongLongUnsignedInt: value = (long long unsigned int) it->second; break;
156                //                      // TODO: value should be able to handle long long unsigned int
157
158                //                      default:
159                //                      value = it->second;
160                //              }
161                //      }
162                // } else {
163                        value = it->second;
164                //}
165
166                return true;
167        }
168        return false;
169}
170
171}
172
173// Local Variables: //
174// tab-width: 4 //
175// mode: c++ //
176// compile-command: "make install" //
177// End: //
Note: See TracBrowser for help on using the repository browser.