| 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 | // AggregateDecl.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sun May 17 23:56:39 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Fri Aug  4 14:22:00 2017 | 
|---|
| 13 | // Update Count     : 22 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 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 | 
|---|
| 23 | #include "Parser/LinkageSpec.h"  // for Spec, linkageName, Cforall | 
|---|
| 24 | #include "Type.h"                // for Type, Type::StorageClasses | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) { | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) { | 
|---|
| 31 | cloneAll( other.members, members ); | 
|---|
| 32 | cloneAll( other.parameters, parameters ); | 
|---|
| 33 | cloneAll( other.attributes, attributes ); | 
|---|
| 34 | body = other.body; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | AggregateDecl::~AggregateDecl() { | 
|---|
| 38 | deleteAll( attributes ); | 
|---|
| 39 | deleteAll( parameters ); | 
|---|
| 40 | deleteAll( members ); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | void AggregateDecl::print( std::ostream &os, Indenter indent ) const { | 
|---|
| 44 | using std::string; | 
|---|
| 45 | using std::endl; | 
|---|
| 46 |  | 
|---|
| 47 | os << typeString() << " " << name << ":"; | 
|---|
| 48 | if ( get_linkage() != LinkageSpec::Cforall ) { | 
|---|
| 49 | os << " " << LinkageSpec::linkageName( linkage ); | 
|---|
| 50 | } // if | 
|---|
| 51 | os << " with body " << has_body(); | 
|---|
| 52 |  | 
|---|
| 53 | if ( ! parameters.empty() ) { | 
|---|
| 54 | os << endl << indent << "... with parameters" << endl; | 
|---|
| 55 | printAll( parameters, os, indent+1 ); | 
|---|
| 56 | } // if | 
|---|
| 57 | if ( ! members.empty() ) { | 
|---|
| 58 | os << endl << indent << "... with members" << endl; | 
|---|
| 59 | printAll( members, os, indent+1 ); | 
|---|
| 60 | } // if | 
|---|
| 61 | if ( ! attributes.empty() ) { | 
|---|
| 62 | os << endl << indent << "... with attributes" << endl; | 
|---|
| 63 | printAll( attributes, os, indent+1 ); | 
|---|
| 64 | } // if | 
|---|
| 65 | os << endl; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const { | 
|---|
| 69 | using std::string; | 
|---|
| 70 | using std::endl; | 
|---|
| 71 |  | 
|---|
| 72 | os << typeString() << " " << name << " with body " << has_body() << endl; | 
|---|
| 73 |  | 
|---|
| 74 | if ( ! parameters.empty() ) { | 
|---|
| 75 | os << indent << "... with parameters" << endl; | 
|---|
| 76 | printAll( parameters, os, indent+1 ); | 
|---|
| 77 | } // if | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | std::string StructDecl::typeString() const { return "struct"; } | 
|---|
| 81 |  | 
|---|
| 82 | std::string UnionDecl::typeString() const { return "union"; } | 
|---|
| 83 |  | 
|---|
| 84 | std::string EnumDecl::typeString() const { return "enum"; } | 
|---|
| 85 |  | 
|---|
| 86 | std::string TraitDecl::typeString() const { return "trait"; } | 
|---|
| 87 |  | 
|---|
| 88 | namespace { | 
|---|
| 89 | long long int getConstValue( Expression * expr ) { | 
|---|
| 90 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( expr ) ) { | 
|---|
| 91 | return getConstValue( castExpr->arg ); | 
|---|
| 92 | } else if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) { | 
|---|
| 93 | return constExpr->intValue(); | 
|---|
| 94 | // can be -1, +1, etc. | 
|---|
| 95 | // } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { | 
|---|
| 96 | //      if ( untypedExpr-> ) | 
|---|
| 97 | } else { | 
|---|
| 98 | assertf( false, "Unhandled expression type in getConstValue for enumerators: %s", toString( expr ).c_str() ); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) { | 
|---|
| 104 | if ( enumValues.empty() ) { | 
|---|
| 105 | long long int currentValue = 0; | 
|---|
| 106 | for ( Declaration * member : members ) { | 
|---|
| 107 | ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member ); | 
|---|
| 108 | if ( field->init ) { | 
|---|
| 109 | SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init ); | 
|---|
| 110 | currentValue = getConstValue( init->value ); | 
|---|
| 111 | } | 
|---|
| 112 | assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() ); | 
|---|
| 113 | enumValues[ field->name ] = currentValue; | 
|---|
| 114 | ++currentValue; | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 | if ( enumValues.count( enumerator->name ) ) { | 
|---|
| 118 | value = enumValues[ enumerator->name ]; | 
|---|
| 119 | return true; | 
|---|
| 120 | } | 
|---|
| 121 | return false; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | // Local Variables: // | 
|---|
| 125 | // tab-width: 4 // | 
|---|
| 126 | // mode: c++ // | 
|---|
| 127 | // compile-command: "make install" // | 
|---|
| 128 | // End: // | 
|---|