source: src/InitTweak/InitTweak.h @ 3cc1111

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 3cc1111 was 3cc1111, checked in by Andrew Beach <ajbeach@…>, 2 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
Line 
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 : Fri Nov 19 14:18:00 2021
13// Update Count     : 7
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
27namespace 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 isCopyFunction( const ast::FunctionDecl * decl );
34
35        /// returns the base type of the first parameter to a constructor/destructor/assignment function
36        Type * getTypeofThis( FunctionType * ftype );
37        const ast::Type * getTypeofThis( const ast::FunctionType * ftype );
38
39        /// returns the first parameter of a constructor/destructor/assignment function
40        ObjectDecl * getParamThis( FunctionType * ftype );
41        const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func);
42
43        /// generate a bitwise assignment operation.
44        ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src );
45
46        ast::Expr * createBitwiseAssignment( const ast::Expr * dst, const ast::Expr * src);
47
48        /// transform Initializer into an argument list that can be passed to a call expression
49        std::list< Expression * > makeInitList( Initializer * init );
50        std::vector< ast::ptr< ast::Expr > > makeInitList( const ast::Init * init );
51
52        /// True if the resolver should try to construct dwt
53        bool tryConstruct( DeclarationWithType * dwt );
54        bool tryConstruct( const ast::DeclWithType * dwt );
55
56        /// True if the type can have a user-defined constructor
57        bool isConstructable( Type * t );
58        bool isConstructable( const ast::Type * t );
59
60        /// True if the Initializer contains designations
61        bool isDesignated( Initializer * init );
62        bool isDesignated( const ast::Init * init );
63
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 );
67        bool checkInitDepth( const ast::ObjectDecl * objDecl );
68
69        /// returns the declaration of the function called by the expr (must be ApplicationExpr or UntypedExpr)
70        DeclarationWithType * getFunction( Expression * expr );
71        const DeclarationWithType * getFunction( const Expression * expr );
72        const ast::DeclWithType * getFunction( const ast::Expr * expr );
73
74        /// Non-Null if expr is a call expression whose target function is intrinsic
75        ApplicationExpr * isIntrinsicCallExpr( Expression * expr );
76        const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr);
77
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.
81        bool isIntrinsicSingleArgCallStmt( Statement * stmt );
82        bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt );
83
84        /// True if stmt is a call statement where the function called is intrinsic.
85        bool isIntrinsicCallStmt( Statement * stmt );
86
87        /// get all Ctor/Dtor call expressions from a Statement
88        void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches );
89        std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt );
90
91        /// get the Ctor/Dtor call expression from a Statement that looks like a generated ctor/dtor call
92        Expression * getCtorDtorCall( Statement * stmt );
93
94        /// returns the name of the function being called
95        std::string getFunctionName( Expression * expr );
96        std::string getFunctionName( const ast::Expr * expr );
97
98        /// returns the argument to a call expression in position N indexed from 0
99        Expression *& getCallArg( Expression * callExpr, unsigned int pos );
100        const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos );
101
102        /// returns the base type of a PointerType or ArrayType, else returns NULL
103        Type * getPointerBase( Type * );
104        const ast::Type* getPointerBase( const ast::Type* );
105
106        /// returns the argument if it is a PointerType or ArrayType, else returns NULL
107        Type * isPointerType( Type * );
108
109        /// returns true if expr is trivially a compile-time constant
110        bool isConstExpr( Expression * expr );
111        bool isConstExpr( Initializer * init );
112
113        bool isConstExpr( const ast::Expr * expr );
114        bool isConstExpr( const ast::Init * init );
115
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
127        void addDataSectionAttribute( ast::ObjectDecl * objDecl );
128
129        class InitExpander_old {
130        public:
131                // expand by stepping through init to get each list of arguments
132                InitExpander_old( Initializer * init );
133
134                // always expand to expr
135                InitExpander_old( Expression * expr );
136
137                // iterator-like interface
138                std::list< Expression * > operator*();
139                InitExpander_old & operator++();
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 );
145                void clearArrayIndices();
146                bool addReference();
147
148                class ExpanderImpl;
149
150                typedef std::list< Expression * > IndexList;
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        };
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
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.
182                /// Mutates callExpr
183                ast::ptr< ast::Stmt > buildListInit( ast::UntypedExpr * callExpr );
184
185                void addArrayIndex( const ast::Expr * index, const ast::Expr * dimension );
186
187                void clearArrayIndices();
188
189                bool addReference();
190        };
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.