source: src/AST/Decl.cpp @ 8a5530c

arm-ehcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8a5530c was 8a5530c, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fixed FunctionType? cast, fixed maybe_accept, implemented statement visitation and fixed several nodes

  • Property mode set to 100644
File size: 3.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 : Aaron B. Moss
12// Last Modified On : Thu May 9 10:00:00 2019
13// Update Count     : 1
14//
15
16#include "Decl.hpp"
17
18#include <cassert>             // for assert, strict_dynamic_cast
19#include <string>
20#include <unordered_map>
21
22#include "Fwd.hpp"             // for UniqueId
23#include "Init.hpp"
24#include "Node.hpp"            // for readonly
25#include "Parser/ParseNode.h"  // for DeclarationNode
26
27namespace ast {
28
29// To canonicalize declarations
30static UniqueId lastUniqueId = 0;
31
32using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
33static IdMapType idMap;
34
35void Decl::fixUniqueId() {
36        if ( uniqueId ) return;  // ensure only set once
37        uniqueId = ++lastUniqueId;
38        idMap[ uniqueId ] = this;
39}
40
41readonly<Decl> Decl::fromId( UniqueId id ) {
42        IdMapType::const_iterator i = idMap.find( id );
43        if ( i != idMap.end() ) return i->second;
44        return {};
45}
46
47// --- FunctionDecl
48
49const Type * FunctionDecl::get_type() const override { return type.get(); }
50void FunctionDecl::set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
51
52// --- TypeDecl
53
54std::string TypeDecl::typeString() const {
55        static const std::string kindNames[] = { "object type", "function type", "tuple type" };
56        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1,
57                "typeString: kindNames is out of sync." );
58        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
59        return (sized ? "sized " : "") + kindNames[ kind ];
60}
61
62std::string TypeDecl::genTypeString() const {
63        static const std::string kindNames[] = { "dtype", "ftype", "ttype" };
64        assertf( sizeof(kindNames)/sizeof(kindNames[0]) == DeclarationNode::NoTypeClass-1, "genTypeString: kindNames is out of sync." );
65        assertf( kind < sizeof(kindNames)/sizeof(kindNames[0]), "TypeDecl's kind is out of bounds." );
66        return kindNames[ kind ];
67}
68
69// --- EnumDecl
70
71bool EnumDecl::valueOf( Decl* enumerator, long long& value ) const {
72        if ( enumValues.empty() ) {
73                long long crntVal = 0;
74                for ( const Decl* member : members ) {
75                        const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
76                        if ( field->init ) {
77                                const SingleInit* init = strict_dynamic_cast< const SingleInit* >( field->init );
78                                auto result = eval( init->value );
79                                if ( ! result.second ) {
80                                        SemanticError( init->location, toString( "Non-constexpr in initialization of "
81                                                "enumerator: ", field ) );
82                                }
83                                crntVal = result.first;
84                        }
85                        if ( enumValues.count( field->name ) != 0 ) {
86                                SemanticError( location, toString( "Enum ", name, " has multiple members with the "     "name ", field->name ) );
87                        }
88                        enumValues[ field->name ] = crntVal;
89                        ++crntVal;
90                }
91        }
92
93        auto it = enumValues.find( enumerator->name );
94        if ( it != enumValues.end() ) {
95                value = it->second;
96                return true;
97        }
98        return false;
99}
100
101}
102
103// Local Variables: //
104// tab-width: 4 //
105// mode: c++ //
106// compile-command: "make install" //
107// End: //
Note: See TracBrowser for help on using the repository browser.