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