source: src/InitTweak/InitTweak.h@ b5d51b0

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 b5d51b0 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
RevLine 
[2b46a13]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//
[2d11663]7// InitTweak.h --
[2b46a13]8//
9// Author : Rob Schluntz
10// Created On : Fri May 13 11:26:36 2016
[335d81f]11// Last Modified By : Andrew Beach
12// Last Modified On : Fri Jul 19 14:18:00 2019
13// Update Count : 6
[2b46a13]14//
15
[6b0b624]16#pragma once
[2b46a13]17
[d180746]18#include <list> // for list
[be9288a]19#include <memory> // for shared_ptr
[d180746]20#include <string> // for string, allocator
[2d11663]21#include <vector>
[2b46a13]22
[9e1d485]23#include "AST/Fwd.hpp" // for AST nodes
[d180746]24#include "SynTree/SynTree.h" // for Visitor Nodes
[2b46a13]25
26// helper functions for initialization
27namespace InitTweak {
[6f096d2]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 );
[d76c588]33 bool isCopyFunction( const ast::FunctionDecl * decl );
[4d4882a]34
[7fc7cdb]35 /// returns the base type of the first parameter to a constructor/destructor/assignment function
[549c006]36 Type * getTypeofThis( FunctionType * ftype );
[7fc7cdb]37
38 /// returns the first parameter of a constructor/destructor/assignment function
[549c006]39 ObjectDecl * getParamThis( FunctionType * ftype );
[490fb92e]40 const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func);
[7fc7cdb]41
[f5c3b6c]42 /// generate a bitwise assignment operation.
43 ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src );
44
[490fb92e]45 ast::Expr * createBitwiseAssignment( const ast::Expr * dst, const ast::Expr * src);
46
[b81adcc4]47 /// transform Initializer into an argument list that can be passed to a call expression
48 std::list< Expression * > makeInitList( Initializer * init );
[b8524ca]49 std::vector< ast::ptr< ast::Expr > > makeInitList( const ast::Init * init );
[2b46a13]50
[22bc276]51 /// True if the resolver should try to construct dwt
52 bool tryConstruct( DeclarationWithType * dwt );
[490fb92e]53 bool tryConstruct( const ast::DeclWithType * dwt );
[2b46a13]54
[29bc63e]55 /// True if the type can have a user-defined constructor
56 bool isConstructable( Type * t );
[490fb92e]57 bool isConstructable( const ast::Type * t );
[29bc63e]58
[b81adcc4]59 /// True if the Initializer contains designations
60 bool isDesignated( Initializer * init );
[2b46a13]61
[dcd73d1]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
[b7b8674]66 /// returns the declaration of the function called by the expr (must be ApplicationExpr or UntypedExpr)
67 DeclarationWithType * getFunction( Expression * expr );
[335d81f]68 const DeclarationWithType * getFunction( const Expression * expr );
[9d6e7fa9]69 const ast::DeclWithType * getFunction( const ast::Expr * expr );
[b7b8674]70
71 /// Non-Null if expr is a call expression whose target function is intrinsic
72 ApplicationExpr * isIntrinsicCallExpr( Expression * expr );
[2d11663]73 const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr);
[aedfd91]74
[b81adcc4]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.
[a465caff]78 bool isIntrinsicSingleArgCallStmt( Statement * stmt );
[2d11663]79 bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt );
[a465caff]80
81 /// True if stmt is a call statement where the function called is intrinsic.
82 bool isIntrinsicCallStmt( Statement * stmt );
[70f89d00]83
[4d2434a]84 /// get all Ctor/Dtor call expressions from a Statement
85 void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches );
[490fb92e]86 std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt );
[4d2434a]87
[b81adcc4]88 /// get the Ctor/Dtor call expression from a Statement that looks like a generated ctor/dtor call
89 Expression * getCtorDtorCall( Statement * stmt );
[f1b1e4c]90
[b81adcc4]91 /// returns the name of the function being called
92 std::string getFunctionName( Expression * expr );
[d7aa12c]93 std::string getFunctionName( const ast::Expr * expr );
[f1b1e4c]94
[b81adcc4]95 /// returns the argument to a call expression in position N indexed from 0
96 Expression *& getCallArg( Expression * callExpr, unsigned int pos );
[9b4f329]97 const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos );
[10a7775]98
[b81adcc4]99 /// returns the base type of a PointerType or ArrayType, else returns NULL
100 Type * getPointerBase( Type * );
[9e1d485]101 const ast::Type* getPointerBase( const ast::Type* );
[64071c2]102
[b81adcc4]103 /// returns the argument if it is a PointerType or ArrayType, else returns NULL
104 Type * isPointerType( Type * );
[5f98ce5]105
[b81adcc4]106 /// returns true if expr is trivially a compile-time constant
107 bool isConstExpr( Expression * expr );
108 bool isConstExpr( Initializer * init );
[39f84a4]109
[f1791a4]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
[7d651a66]121 void addDataSectionAttribute( ast::ObjectDecl * objDecl );
122
[b8524ca]123 class InitExpander_old {
[39f84a4]124 public:
125 // expand by stepping through init to get each list of arguments
[b8524ca]126 InitExpander_old( Initializer * init );
[39f84a4]127
128 // always expand to expr
[b8524ca]129 InitExpander_old( Expression * expr );
[39f84a4]130
131 // iterator-like interface
132 std::list< Expression * > operator*();
[b8524ca]133 InitExpander_old & operator++();
[39f84a4]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 );
[4d2434a]139 void clearArrayIndices();
[1a5ad8c]140 bool addReference();
[39f84a4]141
142 class ExpanderImpl;
[d180746]143
[62e5546]144 typedef std::list< Expression * > IndexList;
[39f84a4]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 };
[b8524ca]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
[6f096d2]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.
[c1ed2ee]176 /// Mutates callExpr
177 ast::ptr< ast::Stmt > buildListInit( ast::UntypedExpr * callExpr );
[b8524ca]178
179 void addArrayIndex( const ast::Expr * index, const ast::Expr * dimension );
180
181 void clearArrayIndices();
182
183 bool addReference();
184 };
[2b46a13]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.