source: src/SynTree/FunctionDecl.cc @ 075734f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 075734f was faddbd8, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

more refactoring of parser code, new tuple syntax

  • Property mode set to 100644
File size: 3.4 KB
Line 
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
25FunctionDecl::FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, std::list< Attribute * > attributes )
26                : Parent( name, sc, linkage, attributes ), type( type ), statements( statements ) {
27        set_isInline( isInline );
28        set_isNoreturn( isNoreturn );
29        // this is a brazen hack to force the function "main" to have C linkage
30        if ( name == "main" ) {
31                set_linkage( LinkageSpec::C );
32        } // if
33}
34
35FunctionDecl::FunctionDecl( const FunctionDecl &other )
36        : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) {
37}
38
39FunctionDecl::~FunctionDecl() {
40        delete type;
41        delete statements;
42        deleteAll( oldDecls );
43}
44
45Type * FunctionDecl::get_type() const {
46        return type;
47}
48
49void FunctionDecl::set_type( Type *t ) {
50        type = dynamic_cast< FunctionType* >( t );
51        assert( type );
52}
53
54void 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::linkageName( 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( get_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
100void 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: //
Note: See TracBrowser for help on using the repository browser.