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