source: src/SynTree/AggregateDecl.cc @ 8a97248

ADTast-experimental
Last change on this file since 8a97248 was 1ed3fe7c, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

change semantic error message for non-const enumerator to be clearer

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[0dd3a2f]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//
[79970ed]7// AggregateDecl.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 23:56:39 2015
[312029a]11// Last Modified By : Peter A. Buhr
[1ed3fe7c]12// Last Modified On : Fri Jul  1 09:12:33 2022
13// Update Count     : 32
[0dd3a2f]14//
[51b7345]15
[ea6332d]16#include <list>                  // for list
17#include <ostream>               // for operator<<, basic_ostream, ostream
18#include <string>                // for operator<<, string, char_traits
19
20#include "Attribute.h"           // for Attribute
21#include "Common/utility.h"      // for printAll, cloneAll, deleteAll
22#include "Declaration.h"         // for AggregateDecl, TypeDecl, Declaration
[69c5c00]23#include "Expression.h"
[312029a]24#include "Initializer.h"
[07de76b]25#include "LinkageSpec.h"         // for Spec, linkageName, Cforall
[ea6332d]26#include "Type.h"                // for Type, Type::StorageClasses
[51b7345]27
28
[312029a]29// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
30static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
31
32const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
33        return aggregateNames[aggr];
34}
35
[fa4805f]36AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) {
[51b7345]37}
38
[0dd3a2f]39AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) {
[a08ba92]40        cloneAll( other.members, members );
41        cloneAll( other.parameters, parameters );
[c0aa336]42        cloneAll( other.attributes, attributes );
[5d125e4]43        body = other.body;
[51b7345]44}
45
[0dd3a2f]46AggregateDecl::~AggregateDecl() {
[c0aa336]47        deleteAll( attributes );
[a08ba92]48        deleteAll( parameters );
[c0aa336]49        deleteAll( members );
[51b7345]50}
51
[50377a4]52void AggregateDecl::print( std::ostream &os, Indenter indent ) const {
[a08ba92]53        using std::string;
54        using std::endl;
[51b7345]55
[50377a4]56        os << typeString() << " " << name << ":";
[cbce272]57        if ( get_linkage() != LinkageSpec::Cforall ) {
[d912bed]58                os << " " << LinkageSpec::name( linkage );
[cbce272]59        } // if
[50377a4]60        os << " with body " << has_body();
[a08ba92]61        if ( ! parameters.empty() ) {
[50377a4]62                os << endl << indent << "... with parameters" << endl;
63                printAll( parameters, os, indent+1 );
[a08ba92]64        } // if
65        if ( ! members.empty() ) {
[50377a4]66                os << endl << indent << "... with members" << endl;
67                printAll( members, os, indent+1 );
[a08ba92]68        } // if
[c0aa336]69        if ( ! attributes.empty() ) {
[50377a4]70                os << endl << indent << "... with attributes" << endl;
71                printAll( attributes, os, indent+1 );
[c0aa336]72        } // if
[50377a4]73        os << endl;
[51b7345]74}
75
[50377a4]76void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const {
[a08ba92]77        using std::string;
78        using std::endl;
[51b7345]79
[50377a4]80        os << typeString() << " " << name << " with body " << has_body() << endl;
[5d125e4]81
[a08ba92]82        if ( ! parameters.empty() ) {
[50377a4]83                os << indent << "... with parameters" << endl;
84                printAll( parameters, os, indent+1 );
[a08ba92]85        } // if
[51b7345]86}
87
[312029a]88const char * StructDecl::typeString() const { return aggrString( kind ); }
[51b7345]89
[69c5c00]90StructInstType * StructDecl::makeInst( std::list< Expression * > const & new_parameters ) {
91        std::list< Expression * > copy_parameters;
92        cloneAll( new_parameters, copy_parameters );
[943bfad]93        return makeInst( copy( copy_parameters ) );
[69c5c00]94}
95
96StructInstType * StructDecl::makeInst( std::list< Expression * > && new_parameters ) {
97        assert( parameters.size() == new_parameters.size() );
98        StructInstType * type = new StructInstType( noQualifiers, this );
99        type->parameters = std::move( new_parameters );
100        return type;
101}
102
[312029a]103const char * UnionDecl::typeString() const { return aggrString( Union ); }
[51b7345]104
[312029a]105const char * EnumDecl::typeString() const { return aggrString( Enum ); }
[51b7345]106
[4559b34]107void EnumDecl::print( std::ostream & os, Indenter indent ) const {
108        AggregateDecl::print(os, indent);
109        os << " with base? " << (base? "True" : "False") << std::endl;
110        if ( base ) {
111                os << "Base Type of Enum:" << std::endl;
112                base->print(os, indent);
113        }
114        os <<  std::endl << "End of EnumDecl::print" << std::endl;
115}
116
[312029a]117const char * TraitDecl::typeString() const { return aggrString( Trait ); }
[51b7345]118
[fc9153d]119bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) {
120        if ( enumValues.empty() ) {
121                long long int currentValue = 0;
122                for ( Declaration * member : members ) {
123                        ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member );
124                        if ( field->init ) {
125                                SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init );
[ac3362c]126                                auto result = eval( init->value );
[1ed3fe7c]127                                if ( ! result.second ) SemanticError( init->location, toString( "Enumerator value for '", field, "' is not an integer constant" ) );
[ac3362c]128                                currentValue = result.first;
[fc9153d]129                        }
130                        assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() );
131                        enumValues[ field->name ] = currentValue;
132                        ++currentValue;
133                }
134        }
135        if ( enumValues.count( enumerator->name ) ) {
136                value = enumValues[ enumerator->name ];
137                return true;
138        }
139        return false;
140}
141
[0dd3a2f]142// Local Variables: //
143// tab-width: 4 //
144// mode: c++ //
145// compile-command: "make install" //
146// End: //
Note: See TracBrowser for help on using the repository browser.