source: src/AST/Decl.cpp @ 9490621

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 9490621 was 9490621, checked in by Andrew Beach <ajbeach@…>, 2 years ago

My work in progress implementation of ForallPointerDecay? for Fangren.

  • Property mode set to 100644
File size: 4.8 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        //idMap[ uniqueId ] = this;
42}
43
44readonly<Decl> Decl::fromId( UniqueId id ) {
45        assert( false );
46        IdMapType::const_iterator i = idMap.find( id );
47        if ( i != idMap.end() ) return i->second;
48        return {};
49}
50
51// --- FunctionDecl
52
53FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name, 
54        std::vector<ptr<TypeDecl>>&& forall,
55        std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
56        CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage,
57        std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs)
58: DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
59        type_params(std::move(forall)), stmts( stmts ) {
60        FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs));
61        for (auto & param : this->params) {
62                ftype->params.emplace_back(param->get_type());
63        }
64        for (auto & ret : this->returns) {
65                ftype->returns.emplace_back(ret->get_type());
66        }
67        for (auto & tp : this->type_params) {
68                ftype->forall.emplace_back(new TypeInstType(tp->name, tp));
69                for (auto & ap: tp->assertions) {
70                        ftype->assertions.emplace_back(new VariableExpr(loc, ap));
71                }
72        }
73        this->type = ftype;
74}
75
76
77const Type * FunctionDecl::get_type() const { return type.get(); }
78void FunctionDecl::set_type( const Type * t ) {
79        type = strict_dynamic_cast< const FunctionType * >( t );
80}
81
82// --- TypeDecl
83
84const char * TypeDecl::typeString() const {
85        static const char * kindNames[] = { "sized data type", "sized data type", "sized object type", "sized function type", "sized tuple type", "sized length value" };
86        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
87        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
88        return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0'
89}
90
91const char * TypeDecl::genTypeString() const {
92        static const char * kindNames[] = { "T &", "T *", "T", "(*)", "T ...", "[T]" };
93        static_assert( sizeof(kindNames) / sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
94        assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
95        return kindNames[ kind ];
96}
97
98std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
99        return out << data.kind << ", " << data.isComplete;
100}
101
102// --- AggregateDecl
103
104// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
105static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
106
107const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
108        return aggregateNames[aggr];
109}
110
111// --- EnumDecl
112
113bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
114        if ( enumValues.empty() ) {
115                long long crntVal = 0;
116                for ( const Decl * member : members ) {
117                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
118                        if ( field->init ) {
119                                const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
120                                auto result = eval( init->value );
121                                if ( ! result.second ) {
122                                        SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
123                                                "enumerator: ", field ) );
124                                }
125                                crntVal = result.first;
126                        }
127                        if ( enumValues.count( field->name ) != 0 ) {
128                                SemanticError( location, ::toString( "Enum ", name, " has multiple members with the "   "name ", field->name ) );
129                        }
130                        enumValues[ field->name ] = crntVal;
131                        ++crntVal;
132                }
133        }
134
135        auto it = enumValues.find( enumerator->name );
136        if ( it != enumValues.end() ) {
137                value = it->second;
138                return true;
139        }
140        return false;
141}
142
143}
144
145// Local Variables: //
146// tab-width: 4 //
147// mode: c++ //
148// compile-command: "make install" //
149// End: //
Note: See TracBrowser for help on using the repository browser.