source: src/AST/Decl.cpp@ 954c954

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 954c954 was 7030dab, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Merge branch 'master' into new-ast

  • Property mode set to 100644
File size: 3.8 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
[312029a]11// Last Modified By : Peter A. Buhr
[07de76b]12// Last Modified On : Fri Dec 13 16:23:15 2019
13// Update Count : 20
[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
[87701b6]22#include "Common/utility.h"
23
[360b2e13]24#include "Fwd.hpp" // for UniqueId
[a300e4a]25#include "Init.hpp"
[360b2e13]26#include "Node.hpp" // for readonly
[87701b6]27#include "Type.hpp" // for readonly
[2bb4a01]28
29namespace ast {
[a300e4a]30
[2bb4a01]31// To canonicalize declarations
32static UniqueId lastUniqueId = 0;
33
34using IdMapType = std::unordered_map< UniqueId, readonly<Decl> >;
35static IdMapType idMap;
36
37void Decl::fixUniqueId() {
38 if ( uniqueId ) return; // ensure only set once
39 uniqueId = ++lastUniqueId;
40 idMap[ uniqueId ] = this;
41}
42
43readonly<Decl> Decl::fromId( UniqueId id ) {
44 IdMapType::const_iterator i = idMap.find( id );
45 if ( i != idMap.end() ) return i->second;
46 return {};
47}
[a300e4a]48
[8a5530c]49// --- FunctionDecl
50
[87701b6]51const Type * FunctionDecl::get_type() const { return type.get(); }
[e0e9a0b]52void FunctionDecl::set_type( const Type * t ) {
53 type = strict_dynamic_cast< const FunctionType * >( t );
54}
[8a5530c]55
[360b2e13]56// --- TypeDecl
57
[312029a]58const char * TypeDecl::typeString() const {
[07de76b]59 static const char * kindNames[] = { "sized data type", "sized object type", "sized function type", "sized tuple type" };
60 static_assert( sizeof(kindNames)/sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "typeString: kindNames is out of sync." );
61 assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
[312029a]62 return sized ? kindNames[ kind ] : &kindNames[ kind ][ sizeof("sized") ]; // sizeof includes '\0'
[360b2e13]63}
64
[312029a]65const char * TypeDecl::genTypeString() const {
[07de76b]66 static const char * kindNames[] = { "dtype", "otype", "ftype", "ttype" };
67 static_assert( sizeof(kindNames)/sizeof(kindNames[0]) == TypeDecl::NUMBER_OF_KINDS, "genTypeString: kindNames is out of sync." );
68 assertf( kind < TypeDecl::NUMBER_OF_KINDS, "TypeDecl kind is out of bounds." );
[360b2e13]69 return kindNames[ kind ];
70}
71
[d76c588]72std::ostream & operator<< ( std::ostream & out, const TypeDecl::Data & data ) {
73 return out << data.kind << ", " << data.isComplete;
74}
75
[312029a]76// --- AggregateDecl
77
78// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
79static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
80
81const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
82 return aggregateNames[aggr];
83}
84
[a300e4a]85// --- EnumDecl
86
[9d6e7fa9]87bool EnumDecl::valueOf( const Decl * enumerator, long long& value ) const {
[a300e4a]88 if ( enumValues.empty() ) {
89 long long crntVal = 0;
[9d6e7fa9]90 for ( const Decl * member : members ) {
[a300e4a]91 const ObjectDecl* field = strict_dynamic_cast< const ObjectDecl* >( member );
92 if ( field->init ) {
[87701b6]93 const SingleInit * init = strict_dynamic_cast< const SingleInit* >( field->init.get() );
[a300e4a]94 auto result = eval( init->value );
95 if ( ! result.second ) {
[733074e]96 SemanticError( init->location, ::toString( "Non-constexpr in initialization of "
[a300e4a]97 "enumerator: ", field ) );
98 }
99 crntVal = result.first;
100 }
101 if ( enumValues.count( field->name ) != 0 ) {
[733074e]102 SemanticError( location, ::toString( "Enum ", name, " has multiple members with the " "name ", field->name ) );
[a300e4a]103 }
104 enumValues[ field->name ] = crntVal;
105 ++crntVal;
106 }
107 }
108
109 auto it = enumValues.find( enumerator->name );
110 if ( it != enumValues.end() ) {
111 value = it->second;
112 return true;
113 }
114 return false;
115}
116
[2bb4a01]117}
118
119// Local Variables: //
120// tab-width: 4 //
121// mode: c++ //
122// compile-command: "make install" //
123// End: //
Note: See TracBrowser for help on using the repository browser.