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