[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
|
---|
| 27 | namespace 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 |
|
---|
[b8524ca] | 121 | class InitExpander_old {
|
---|
[39f84a4] | 122 | public:
|
---|
| 123 | // expand by stepping through init to get each list of arguments
|
---|
[b8524ca] | 124 | InitExpander_old( Initializer * init );
|
---|
[39f84a4] | 125 |
|
---|
| 126 | // always expand to expr
|
---|
[b8524ca] | 127 | InitExpander_old( Expression * expr );
|
---|
[39f84a4] | 128 |
|
---|
| 129 | // iterator-like interface
|
---|
| 130 | std::list< Expression * > operator*();
|
---|
[b8524ca] | 131 | InitExpander_old & operator++();
|
---|
[39f84a4] | 132 |
|
---|
| 133 | // builds statement which has the same semantics as a C-style list initializer
|
---|
| 134 | // (for array initializers) using callExpr as the base expression to perform initialization
|
---|
| 135 | Statement * buildListInit( UntypedExpr * callExpr );
|
---|
| 136 | void addArrayIndex( Expression * index, Expression * dimension );
|
---|
[4d2434a] | 137 | void clearArrayIndices();
|
---|
[1a5ad8c] | 138 | bool addReference();
|
---|
[39f84a4] | 139 |
|
---|
| 140 | class ExpanderImpl;
|
---|
[d180746] | 141 |
|
---|
[62e5546] | 142 | typedef std::list< Expression * > IndexList;
|
---|
[39f84a4] | 143 | private:
|
---|
| 144 | std::shared_ptr< ExpanderImpl > expander;
|
---|
| 145 | std::list< Expression * > cur;
|
---|
| 146 |
|
---|
| 147 | // invariant: list of size 2N (elements come in pairs [index, dimension])
|
---|
| 148 | IndexList indices;
|
---|
| 149 | };
|
---|
[b8524ca] | 150 |
|
---|
| 151 | class InitExpander_new {
|
---|
| 152 | public:
|
---|
| 153 | using IndexList = std::vector< ast::ptr< ast::Expr > >;
|
---|
| 154 | class ExpanderImpl;
|
---|
| 155 |
|
---|
| 156 | private:
|
---|
| 157 | std::shared_ptr< ExpanderImpl > expander;
|
---|
| 158 | std::vector< ast::ptr< ast::Expr > > crnt;
|
---|
| 159 | // invariant: list of size 2N (elements come in pairs [index, dimension])
|
---|
| 160 | IndexList indices;
|
---|
| 161 |
|
---|
| 162 | public:
|
---|
| 163 | /// Expand by stepping through init to get each list of arguments
|
---|
| 164 | InitExpander_new( const ast::Init * init );
|
---|
| 165 |
|
---|
| 166 | /// Always expand to expression
|
---|
| 167 | InitExpander_new( const ast::Expr * expr );
|
---|
| 168 |
|
---|
| 169 | std::vector< ast::ptr< ast::Expr > > operator* ();
|
---|
| 170 | InitExpander_new & operator++ ();
|
---|
| 171 |
|
---|
[6f096d2] | 172 | /// builds statement which has the same semantics as a C-style list initializer (for array
|
---|
| 173 | /// initializers) using callExpr as the base expression to perform initialization.
|
---|
[c1ed2ee] | 174 | /// Mutates callExpr
|
---|
| 175 | ast::ptr< ast::Stmt > buildListInit( ast::UntypedExpr * callExpr );
|
---|
[b8524ca] | 176 |
|
---|
| 177 | void addArrayIndex( const ast::Expr * index, const ast::Expr * dimension );
|
---|
| 178 |
|
---|
| 179 | void clearArrayIndices();
|
---|
| 180 |
|
---|
| 181 | bool addReference();
|
---|
| 182 | };
|
---|
[2b46a13] | 183 | } // namespace
|
---|
| 184 |
|
---|
| 185 | // Local Variables: //
|
---|
| 186 | // tab-width: 4 //
|
---|
| 187 | // mode: c++ //
|
---|
| 188 | // compile-command: "make install" //
|
---|
| 189 | // End: //
|
---|