source: src/InitTweak/InitTweak.h@ be5e34b

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since be5e34b was 7d651a66, checked in by Fangren Yu <f37yu@…>, 5 years ago

fix static init crash

  • Property mode set to 100644
File size: 7.3 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// InitTweak.h --
8//
9// Author : Rob Schluntz
10// Created On : Fri May 13 11:26:36 2016
11// Last Modified By : Andrew Beach
12// Last Modified On : Fri Jul 19 14:18:00 2019
13// Update Count : 6
14//
15
16#pragma once
17
18#include <list> // for list
19#include <memory> // for shared_ptr
20#include <string> // for string, allocator
21#include <vector>
22
23#include "AST/Fwd.hpp" // for AST nodes
24#include "SynTree/SynTree.h" // for Visitor Nodes
25
26// helper functions for initialization
27namespace InitTweak {
28 const FunctionDecl * isAssignment( const Declaration * decl );
29 const FunctionDecl * isDestructor( const Declaration * decl );
30 const FunctionDecl * isDefaultConstructor( const Declaration * decl );
31 const FunctionDecl * isCopyConstructor( const Declaration * decl );
32 const FunctionDecl * isCopyFunction( const Declaration * decl, const std::string & fname );
33 bool isCopyFunction( const ast::FunctionDecl * decl );
34
35 /// returns the base type of the first parameter to a constructor/destructor/assignment function
36 Type * getTypeofThis( FunctionType * ftype );
37
38 /// returns the first parameter of a constructor/destructor/assignment function
39 ObjectDecl * getParamThis( FunctionType * ftype );
40 const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func);
41
42 /// generate a bitwise assignment operation.
43 ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src );
44
45 ast::Expr * createBitwiseAssignment( const ast::Expr * dst, const ast::Expr * src);
46
47 /// transform Initializer into an argument list that can be passed to a call expression
48 std::list< Expression * > makeInitList( Initializer * init );
49 std::vector< ast::ptr< ast::Expr > > makeInitList( const ast::Init * init );
50
51 /// True if the resolver should try to construct dwt
52 bool tryConstruct( DeclarationWithType * dwt );
53 bool tryConstruct( const ast::DeclWithType * dwt );
54
55 /// True if the type can have a user-defined constructor
56 bool isConstructable( Type * t );
57 bool isConstructable( const ast::Type * t );
58
59 /// True if the Initializer contains designations
60 bool isDesignated( Initializer * init );
61
62 /// True if the ObjectDecl's Initializer nesting level is not deeper than the depth of its
63 /// type, where the depth of its type is the number of nested ArrayTypes + 1
64 bool checkInitDepth( ObjectDecl * objDecl );
65
66 /// returns the declaration of the function called by the expr (must be ApplicationExpr or UntypedExpr)
67 DeclarationWithType * getFunction( Expression * expr );
68 const DeclarationWithType * getFunction( const Expression * expr );
69 const ast::DeclWithType * getFunction( const ast::Expr * expr );
70
71 /// Non-Null if expr is a call expression whose target function is intrinsic
72 ApplicationExpr * isIntrinsicCallExpr( Expression * expr );
73 const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr);
74
75 /// True if stmt is a call statement where the function called is intrinsic and takes one parameter.
76 /// Intended to be used for default ctor/dtor calls, but might have use elsewhere.
77 /// Currently has assertions that make it less than fully general.
78 bool isIntrinsicSingleArgCallStmt( Statement * stmt );
79 bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt );
80
81 /// True if stmt is a call statement where the function called is intrinsic.
82 bool isIntrinsicCallStmt( Statement * stmt );
83
84 /// get all Ctor/Dtor call expressions from a Statement
85 void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches );
86 std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt );
87
88 /// get the Ctor/Dtor call expression from a Statement that looks like a generated ctor/dtor call
89 Expression * getCtorDtorCall( Statement * stmt );
90
91 /// returns the name of the function being called
92 std::string getFunctionName( Expression * expr );
93 std::string getFunctionName( const ast::Expr * expr );
94
95 /// returns the argument to a call expression in position N indexed from 0
96 Expression *& getCallArg( Expression * callExpr, unsigned int pos );
97 const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos );
98
99 /// returns the base type of a PointerType or ArrayType, else returns NULL
100 Type * getPointerBase( Type * );
101 const ast::Type* getPointerBase( const ast::Type* );
102
103 /// returns the argument if it is a PointerType or ArrayType, else returns NULL
104 Type * isPointerType( Type * );
105
106 /// returns true if expr is trivially a compile-time constant
107 bool isConstExpr( Expression * expr );
108 bool isConstExpr( Initializer * init );
109
110 /// Modifies objDecl to have:
111 /// __attribute__((section (".data#")))
112 /// which makes gcc put the declared variable in the data section,
113 /// which is helpful for global constants on newer gcc versions,
114 /// so that CFA's generated initialization won't segfault when writing it via a const cast.
115 /// The trailing # is an injected assembly comment, to suppress the "a" in
116 /// .section .data,"a"
117 /// .section .data#,"a"
118 /// to avoid assembler warning "ignoring changed section attributes for .data"
119 void addDataSectonAttribute( ObjectDecl * objDecl );
120
121 void addDataSectionAttribute( ast::ObjectDecl * objDecl );
122
123 class InitExpander_old {
124 public:
125 // expand by stepping through init to get each list of arguments
126 InitExpander_old( Initializer * init );
127
128 // always expand to expr
129 InitExpander_old( Expression * expr );
130
131 // iterator-like interface
132 std::list< Expression * > operator*();
133 InitExpander_old & operator++();
134
135 // builds statement which has the same semantics as a C-style list initializer
136 // (for array initializers) using callExpr as the base expression to perform initialization
137 Statement * buildListInit( UntypedExpr * callExpr );
138 void addArrayIndex( Expression * index, Expression * dimension );
139 void clearArrayIndices();
140 bool addReference();
141
142 class ExpanderImpl;
143
144 typedef std::list< Expression * > IndexList;
145 private:
146 std::shared_ptr< ExpanderImpl > expander;
147 std::list< Expression * > cur;
148
149 // invariant: list of size 2N (elements come in pairs [index, dimension])
150 IndexList indices;
151 };
152
153 class InitExpander_new {
154 public:
155 using IndexList = std::vector< ast::ptr< ast::Expr > >;
156 class ExpanderImpl;
157
158 private:
159 std::shared_ptr< ExpanderImpl > expander;
160 std::vector< ast::ptr< ast::Expr > > crnt;
161 // invariant: list of size 2N (elements come in pairs [index, dimension])
162 IndexList indices;
163
164 public:
165 /// Expand by stepping through init to get each list of arguments
166 InitExpander_new( const ast::Init * init );
167
168 /// Always expand to expression
169 InitExpander_new( const ast::Expr * expr );
170
171 std::vector< ast::ptr< ast::Expr > > operator* ();
172 InitExpander_new & operator++ ();
173
174 /// builds statement which has the same semantics as a C-style list initializer (for array
175 /// initializers) using callExpr as the base expression to perform initialization.
176 /// Mutates callExpr
177 ast::ptr< ast::Stmt > buildListInit( ast::UntypedExpr * callExpr );
178
179 void addArrayIndex( const ast::Expr * index, const ast::Expr * dimension );
180
181 void clearArrayIndices();
182
183 bool addReference();
184 };
185} // namespace
186
187// Local Variables: //
188// tab-width: 4 //
189// mode: c++ //
190// compile-command: "make install" //
191// End: //
Note: See TracBrowser for help on using the repository browser.