source: src/InitTweak/InitTweak.h @ afe2939

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since afe2939 was f1791a4, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Expanding earlier #182 fix to another case of the same issue. Fixes #224?

There is now a reusable helper function addDataSectonAttribute that applies the fix to an ObjectDecl? when we realize it needs it. Test const-init is expanded with case of #224.

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