source: src/InitTweak/InitTweak.h @ 6b2d444

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 6b2d444 was 3cc1111, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Small fix in Decl.hpp and a new-ast function added in InitTweak?.

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