[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
|
---|
[d912bed] | 12 | // Last Modified On : Mon Dec 16 15:07:20 2019
|
---|
| 13 | // Update Count : 31
|
---|
[0dd3a2f] | 14 | //
|
---|
[51b73452] | 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
|
---|
[51b73452] | 27 |
|
---|
| 28 |
|
---|
[312029a] | 29 | // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
|
---|
| 30 | static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
|
---|
| 31 |
|
---|
| 32 | const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
|
---|
| 33 | return aggregateNames[aggr];
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[fa4805f] | 36 | AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) {
|
---|
[51b73452] | 37 | }
|
---|
| 38 |
|
---|
[0dd3a2f] | 39 | AggregateDecl::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;
|
---|
[51b73452] | 44 | }
|
---|
| 45 |
|
---|
[0dd3a2f] | 46 | AggregateDecl::~AggregateDecl() {
|
---|
[c0aa336] | 47 | deleteAll( attributes );
|
---|
[a08ba92] | 48 | deleteAll( parameters );
|
---|
[c0aa336] | 49 | deleteAll( members );
|
---|
[51b73452] | 50 | }
|
---|
| 51 |
|
---|
[50377a4] | 52 | void AggregateDecl::print( std::ostream &os, Indenter indent ) const {
|
---|
[a08ba92] | 53 | using std::string;
|
---|
| 54 | using std::endl;
|
---|
[51b73452] | 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();
|
---|
[5d125e4] | 61 |
|
---|
[a08ba92] | 62 | if ( ! parameters.empty() ) {
|
---|
[50377a4] | 63 | os << endl << indent << "... with parameters" << endl;
|
---|
| 64 | printAll( parameters, os, indent+1 );
|
---|
[a08ba92] | 65 | } // if
|
---|
| 66 | if ( ! members.empty() ) {
|
---|
[50377a4] | 67 | os << endl << indent << "... with members" << endl;
|
---|
| 68 | printAll( members, os, indent+1 );
|
---|
[a08ba92] | 69 | } // if
|
---|
[c0aa336] | 70 | if ( ! attributes.empty() ) {
|
---|
[50377a4] | 71 | os << endl << indent << "... with attributes" << endl;
|
---|
| 72 | printAll( attributes, os, indent+1 );
|
---|
[c0aa336] | 73 | } // if
|
---|
[50377a4] | 74 | os << endl;
|
---|
[51b73452] | 75 | }
|
---|
| 76 |
|
---|
[50377a4] | 77 | void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const {
|
---|
[a08ba92] | 78 | using std::string;
|
---|
| 79 | using std::endl;
|
---|
[51b73452] | 80 |
|
---|
[50377a4] | 81 | os << typeString() << " " << name << " with body " << has_body() << endl;
|
---|
[5d125e4] | 82 |
|
---|
[a08ba92] | 83 | if ( ! parameters.empty() ) {
|
---|
[50377a4] | 84 | os << indent << "... with parameters" << endl;
|
---|
| 85 | printAll( parameters, os, indent+1 );
|
---|
[a08ba92] | 86 | } // if
|
---|
[51b73452] | 87 | }
|
---|
| 88 |
|
---|
[312029a] | 89 | const char * StructDecl::typeString() const { return aggrString( kind ); }
|
---|
[51b73452] | 90 |
|
---|
[69c5c00] | 91 | StructInstType * StructDecl::makeInst( std::list< Expression * > const & new_parameters ) {
|
---|
| 92 | std::list< Expression * > copy_parameters;
|
---|
| 93 | cloneAll( new_parameters, copy_parameters );
|
---|
[943bfad] | 94 | return makeInst( copy( copy_parameters ) );
|
---|
[69c5c00] | 95 | }
|
---|
| 96 |
|
---|
| 97 | StructInstType * StructDecl::makeInst( std::list< Expression * > && new_parameters ) {
|
---|
| 98 | assert( parameters.size() == new_parameters.size() );
|
---|
| 99 | StructInstType * type = new StructInstType( noQualifiers, this );
|
---|
| 100 | type->parameters = std::move( new_parameters );
|
---|
| 101 | return type;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[312029a] | 104 | const char * UnionDecl::typeString() const { return aggrString( Union ); }
|
---|
[51b73452] | 105 |
|
---|
[312029a] | 106 | const char * EnumDecl::typeString() const { return aggrString( Enum ); }
|
---|
[51b73452] | 107 |
|
---|
[312029a] | 108 | const char * TraitDecl::typeString() const { return aggrString( Trait ); }
|
---|
[51b73452] | 109 |
|
---|
[fc9153d] | 110 | bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) {
|
---|
| 111 | if ( enumValues.empty() ) {
|
---|
| 112 | long long int currentValue = 0;
|
---|
| 113 | for ( Declaration * member : members ) {
|
---|
| 114 | ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member );
|
---|
| 115 | if ( field->init ) {
|
---|
| 116 | SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init );
|
---|
[ac3362c] | 117 | auto result = eval( init->value );
|
---|
| 118 | if ( ! result.second ) SemanticError( init->location, toString( "Non-constexpr in initialization of enumerator: ", field ) );
|
---|
| 119 | currentValue = result.first;
|
---|
[fc9153d] | 120 | }
|
---|
| 121 | assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() );
|
---|
| 122 | enumValues[ field->name ] = currentValue;
|
---|
| 123 | ++currentValue;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | if ( enumValues.count( enumerator->name ) ) {
|
---|
| 127 | value = enumValues[ enumerator->name ];
|
---|
| 128 | return true;
|
---|
| 129 | }
|
---|
| 130 | return false;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[0dd3a2f] | 133 | // Local Variables: //
|
---|
| 134 | // tab-width: 4 //
|
---|
| 135 | // mode: c++ //
|
---|
| 136 | // compile-command: "make install" //
|
---|
| 137 | // End: //
|
---|