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