source: src/InitTweak/InitTweak.h @ 2514f68b

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 2514f68b was 16ba4a6f, checked in by Fangren Yu <f37yu@…>, 4 years ago

factor out resolver calls in pre-resolution stage

  • Property mode set to 100644
File size: 7.5 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 );
[16ba4a6f]61        bool isDesignated( const ast::Init * init );
[2b46a13]62
[dcd73d1]63        /// True if the ObjectDecl's Initializer nesting level is not deeper than the depth of its
64        /// type, where the depth of its type is the number of nested ArrayTypes + 1
65        bool checkInitDepth( ObjectDecl * objDecl );
[16ba4a6f]66        bool checkInitDepth( const ast::ObjectDecl * objDecl );
[dcd73d1]67
[b7b8674]68        /// returns the declaration of the function called by the expr (must be ApplicationExpr or UntypedExpr)
69        DeclarationWithType * getFunction( Expression * expr );
[335d81f]70        const DeclarationWithType * getFunction( const Expression * expr );
[9d6e7fa9]71        const ast::DeclWithType * getFunction( const ast::Expr * expr );
[b7b8674]72
73        /// Non-Null if expr is a call expression whose target function is intrinsic
74        ApplicationExpr * isIntrinsicCallExpr( Expression * expr );
[2d11663]75        const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr);
[aedfd91]76
[b81adcc4]77        /// True if stmt is a call statement where the function called is intrinsic and takes one parameter.
78        /// Intended to be used for default ctor/dtor calls, but might have use elsewhere.
79        /// Currently has assertions that make it less than fully general.
[a465caf]80        bool isIntrinsicSingleArgCallStmt( Statement * stmt );
[2d11663]81        bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt );
[a465caf]82
83        /// True if stmt is a call statement where the function called is intrinsic.
84        bool isIntrinsicCallStmt( Statement * stmt );
[70f89d00]85
[4d2434a]86        /// get all Ctor/Dtor call expressions from a Statement
87        void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches );
[490fb92e]88        std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt );
[4d2434a]89
[b81adcc4]90        /// get the Ctor/Dtor call expression from a Statement that looks like a generated ctor/dtor call
91        Expression * getCtorDtorCall( Statement * stmt );
[f1b1e4c]92
[b81adcc4]93        /// returns the name of the function being called
94        std::string getFunctionName( Expression * expr );
[d7aa12c]95        std::string getFunctionName( const ast::Expr * expr );
[f1b1e4c]96
[b81adcc4]97        /// returns the argument to a call expression in position N indexed from 0
98        Expression *& getCallArg( Expression * callExpr, unsigned int pos );
[9b4f329]99        const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos );
[10a7775]100
[b81adcc4]101        /// returns the base type of a PointerType or ArrayType, else returns NULL
102        Type * getPointerBase( Type * );
[9e1d485]103        const ast::Type* getPointerBase( const ast::Type* );
[64071c2]104
[b81adcc4]105        /// returns the argument if it is a PointerType or ArrayType, else returns NULL
106        Type * isPointerType( Type * );
[5f98ce5]107
[b81adcc4]108        /// returns true if expr is trivially a compile-time constant
109        bool isConstExpr( Expression * expr );
110        bool isConstExpr( Initializer * init );
[39f84a4]111
[16ba4a6f]112        bool isConstExpr( const ast::Expr * expr );
113        bool isConstExpr( const ast::Init * init );
114
[f1791a4]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 addDataSectonAttribute( ObjectDecl * objDecl );
125
[7d651a6]126        void addDataSectionAttribute( ast::ObjectDecl * objDecl );
127
[b8524ca]128        class InitExpander_old {
[39f84a4]129        public:
130                // expand by stepping through init to get each list of arguments
[b8524ca]131                InitExpander_old( Initializer * init );
[39f84a4]132
133                // always expand to expr
[b8524ca]134                InitExpander_old( Expression * expr );
[39f84a4]135
136                // iterator-like interface
137                std::list< Expression * > operator*();
[b8524ca]138                InitExpander_old & operator++();
[39f84a4]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 );
[4d2434a]144                void clearArrayIndices();
[1a5ad8c]145                bool addReference();
[39f84a4]146
147                class ExpanderImpl;
[d180746]148
[62e5546]149                typedef std::list< Expression * > IndexList;
[39f84a4]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        };
[b8524ca]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
[6f096d2]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.
[c1ed2ee]181                /// Mutates callExpr
182                ast::ptr< ast::Stmt > buildListInit( ast::UntypedExpr * callExpr );
[b8524ca]183
184                void addArrayIndex( const ast::Expr * index, const ast::Expr * dimension );
185
186                void clearArrayIndices();
187
188                bool addReference();
189        };
[2b46a13]190} // namespace
191
192// Local Variables: //
193// tab-width: 4 //
194// mode: c++ //
195// compile-command: "make install" //
196// End: //
Note: See TracBrowser for help on using the repository browser.