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