[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 | // |
---|
[1f6e009] | 7 | // FunctionDecl.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1f6e009] | 11 | // Last Modified By : Rob Schluntz |
---|
[03e5d14] | 12 | // Last Modified On : Fri May 06 15:41:05 2016 |
---|
[1db21619] | 13 | // Update Count : 19 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <cassert> |
---|
| 17 | |
---|
| 18 | #include "Declaration.h" |
---|
| 19 | #include "Statement.h" |
---|
| 20 | #include "Type.h" |
---|
[d3b7937] | 21 | #include "Common/utility.h" |
---|
[51b7345] | 22 | |
---|
[4e24610] | 23 | FunctionDecl::FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, Attribute attribute ) |
---|
| 24 | : Parent( name, sc, linkage ), type( type ), statements( statements ), attribute( attribute ) { |
---|
[1db21619] | 25 | set_isInline( isInline ); |
---|
| 26 | set_isNoreturn( isNoreturn ); |
---|
[4e24610] | 27 | // this is a brazen hack to force the function "main" to have C linkage |
---|
| 28 | if ( name == "main" ) { |
---|
| 29 | set_linkage( LinkageSpec::C ); |
---|
| 30 | } // if |
---|
[51b7345] | 31 | } |
---|
| 32 | |
---|
| 33 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) |
---|
[4e24610] | 34 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ), attribute( other.attribute ) { |
---|
[51b7345] | 35 | } |
---|
| 36 | |
---|
[c11e31c] | 37 | FunctionDecl::~FunctionDecl() { |
---|
[0dd3a2f] | 38 | delete type; |
---|
| 39 | delete statements; |
---|
[51b7345] | 40 | } |
---|
| 41 | |
---|
[c11e31c] | 42 | Type * FunctionDecl::get_type() const { |
---|
[0dd3a2f] | 43 | return type; |
---|
[51b7345] | 44 | } |
---|
| 45 | |
---|
[c11e31c] | 46 | void FunctionDecl::set_type( Type *t ) { |
---|
[0dd3a2f] | 47 | type = dynamic_cast< FunctionType* >( t ); |
---|
| 48 | assert( type ); |
---|
[51b7345] | 49 | } |
---|
| 50 | |
---|
[c11e31c] | 51 | void FunctionDecl::print( std::ostream &os, int indent ) const { |
---|
[0dd3a2f] | 52 | using std::endl; |
---|
| 53 | using std::string; |
---|
[1f6e009] | 54 | |
---|
[0dd3a2f] | 55 | if ( get_name() != "" ) { |
---|
[5f2f2d7] | 56 | os << get_name() << ": "; |
---|
[0dd3a2f] | 57 | } // if |
---|
| 58 | if ( get_linkage() != LinkageSpec::Cforall ) { |
---|
| 59 | os << LinkageSpec::toString( get_linkage() ) << " "; |
---|
| 60 | } // if |
---|
[1db21619] | 61 | if ( get_isInline() ) { |
---|
[0dd3a2f] | 62 | os << "inline "; |
---|
| 63 | } // if |
---|
[1db21619] | 64 | if ( get_isNoreturn() ) { |
---|
[de62360d] | 65 | os << "_Noreturn "; |
---|
| 66 | } // if |
---|
[03e5d14] | 67 | switch ( attribute.type ) { |
---|
| 68 | case Attribute::Constructor: |
---|
[4e24610] | 69 | os << "Global Constructor "; |
---|
| 70 | break; |
---|
[03e5d14] | 71 | case Attribute::Destructor: |
---|
[4e24610] | 72 | os << "Global Destructor "; |
---|
| 73 | break; |
---|
| 74 | default: |
---|
| 75 | break; |
---|
| 76 | } |
---|
[03e5d14] | 77 | if ( attribute.priority != Attribute::Default ) { |
---|
| 78 | os << "with priority " << attribute.priority << " "; |
---|
| 79 | } |
---|
[68cd1ce] | 80 | if ( get_storageClass() != DeclarationNode::NoStorageClass ) { |
---|
| 81 | os << DeclarationNode::storageName[ get_storageClass() ] << ' '; |
---|
[0dd3a2f] | 82 | } // if |
---|
| 83 | if ( get_type() ) { |
---|
| 84 | get_type()->print( os, indent ); |
---|
| 85 | } else { |
---|
| 86 | os << "untyped entity "; |
---|
| 87 | } // if |
---|
[843054c2] | 88 | |
---|
[0dd3a2f] | 89 | if ( ! oldIdents.empty() ) { |
---|
[de62360d] | 90 | os << string( indent + 2, ' ' ) << "with parameter names" << endl; |
---|
[0dd3a2f] | 91 | for ( std::list< std::string >::const_iterator i = oldIdents.begin(); i != oldIdents.end(); ++i ) { |
---|
[de62360d] | 92 | os << string( indent + 4, ' ' ) << *i << endl; |
---|
[0dd3a2f] | 93 | } // for |
---|
| 94 | } // if |
---|
[843054c2] | 95 | |
---|
[0dd3a2f] | 96 | if ( ! oldDecls.empty() ) { |
---|
[de62360d] | 97 | os << string( indent + 2, ' ' ) << "with parameter declarations" << endl; |
---|
| 98 | printAll( oldDecls, os, indent + 4 ); |
---|
[0dd3a2f] | 99 | } // if |
---|
| 100 | if ( statements ) { |
---|
[de62360d] | 101 | os << string( indent + 2, ' ' ) << "with body " << endl; |
---|
| 102 | statements->print( os, indent + 4 ); |
---|
[0dd3a2f] | 103 | } // if |
---|
[51b7345] | 104 | } |
---|
| 105 | |
---|
[c11e31c] | 106 | void FunctionDecl::printShort( std::ostream &os, int indent ) const { |
---|
[0dd3a2f] | 107 | using std::endl; |
---|
| 108 | using std::string; |
---|
[1f6e009] | 109 | |
---|
[0dd3a2f] | 110 | if ( get_name() != "" ) { |
---|
[5f2f2d7] | 111 | os << get_name() << ": "; |
---|
[0dd3a2f] | 112 | } // if |
---|
[1db21619] | 113 | if ( get_isInline() ) { |
---|
[0dd3a2f] | 114 | os << "inline "; |
---|
| 115 | } // if |
---|
[1db21619] | 116 | if ( get_isNoreturn() ) { |
---|
[de62360d] | 117 | os << "_Noreturn "; |
---|
| 118 | } // if |
---|
[03e5d14] | 119 | switch ( attribute.type ) { |
---|
| 120 | case Attribute::Constructor: |
---|
[4e24610] | 121 | os << " Global Constructor "; |
---|
| 122 | break; |
---|
[03e5d14] | 123 | case Attribute::Destructor: |
---|
[4e24610] | 124 | os << " Global Destructor "; |
---|
| 125 | break; |
---|
| 126 | default: |
---|
| 127 | break; |
---|
| 128 | } |
---|
[03e5d14] | 129 | if ( attribute.priority != Attribute::Default ) { |
---|
| 130 | os << "with priority " << attribute.priority << " "; |
---|
| 131 | } |
---|
[68cd1ce] | 132 | if ( get_storageClass() != DeclarationNode::NoStorageClass ) { |
---|
| 133 | os << DeclarationNode::storageName[ get_storageClass() ] << ' '; |
---|
[0dd3a2f] | 134 | } // if |
---|
| 135 | if ( get_type() ) { |
---|
| 136 | get_type()->print( os, indent ); |
---|
| 137 | } else { |
---|
| 138 | os << "untyped entity "; |
---|
| 139 | } // if |
---|
[51b7345] | 140 | } |
---|
[0dd3a2f] | 141 | |
---|
| 142 | // Local Variables: // |
---|
| 143 | // tab-width: 4 // |
---|
| 144 | // mode: c++ // |
---|
| 145 | // compile-command: "make install" // |
---|
| 146 | // End: // |
---|