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 11:35:09 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 "Common/utility.h" |
---|
22 | |
---|
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 ) { |
---|
25 | set_isInline( isInline ); |
---|
26 | set_isNoreturn( isNoreturn ); |
---|
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 |
---|
31 | } |
---|
32 | |
---|
33 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) |
---|
34 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ), attribute( other.attribute ) { |
---|
35 | } |
---|
36 | |
---|
37 | FunctionDecl::~FunctionDecl() { |
---|
38 | delete type; |
---|
39 | delete statements; |
---|
40 | } |
---|
41 | |
---|
42 | Type * FunctionDecl::get_type() const { |
---|
43 | return type; |
---|
44 | } |
---|
45 | |
---|
46 | void FunctionDecl::set_type( Type *t ) { |
---|
47 | type = dynamic_cast< FunctionType* >( t ); |
---|
48 | assert( type ); |
---|
49 | } |
---|
50 | |
---|
51 | void FunctionDecl::print( std::ostream &os, int indent ) const { |
---|
52 | using std::endl; |
---|
53 | using std::string; |
---|
54 | |
---|
55 | if ( get_name() != "" ) { |
---|
56 | os << get_name() << ": "; |
---|
57 | } // if |
---|
58 | if ( get_linkage() != LinkageSpec::Cforall ) { |
---|
59 | os << LinkageSpec::toString( get_linkage() ) << " "; |
---|
60 | } // if |
---|
61 | if ( get_isInline() ) { |
---|
62 | os << "inline "; |
---|
63 | } // if |
---|
64 | if ( get_isNoreturn() ) { |
---|
65 | os << "_Noreturn "; |
---|
66 | } // if |
---|
67 | switch ( attribute ) { |
---|
68 | case Constructor: |
---|
69 | os << "Global Constructor "; |
---|
70 | break; |
---|
71 | case Destructor: |
---|
72 | os << "Global Destructor "; |
---|
73 | break; |
---|
74 | default: |
---|
75 | break; |
---|
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 | statements->print( os, indent + 4 ); |
---|
100 | } // if |
---|
101 | } |
---|
102 | |
---|
103 | void FunctionDecl::printShort( std::ostream &os, int indent ) const { |
---|
104 | using std::endl; |
---|
105 | using std::string; |
---|
106 | |
---|
107 | if ( get_name() != "" ) { |
---|
108 | os << get_name() << ": "; |
---|
109 | } // if |
---|
110 | if ( get_isInline() ) { |
---|
111 | os << "inline "; |
---|
112 | } // if |
---|
113 | if ( get_isNoreturn() ) { |
---|
114 | os << "_Noreturn "; |
---|
115 | } // if |
---|
116 | switch ( attribute ) { |
---|
117 | case Constructor: |
---|
118 | os << " Global Constructor "; |
---|
119 | break; |
---|
120 | case Destructor: |
---|
121 | os << " Global Destructor "; |
---|
122 | break; |
---|
123 | default: |
---|
124 | break; |
---|
125 | } |
---|
126 | if ( get_storageClass() != DeclarationNode::NoStorageClass ) { |
---|
127 | os << DeclarationNode::storageName[ get_storageClass() ] << ' '; |
---|
128 | } // if |
---|
129 | if ( get_type() ) { |
---|
130 | get_type()->print( os, indent ); |
---|
131 | } else { |
---|
132 | os << "untyped entity "; |
---|
133 | } // if |
---|
134 | } |
---|
135 | |
---|
136 | // Local Variables: // |
---|
137 | // tab-width: 4 // |
---|
138 | // mode: c++ // |
---|
139 | // compile-command: "make install" // |
---|
140 | // End: // |
---|