1 // -*- mode: C++ -*-
2 //
3 // Copyright (c) 2007, 2008, 2010, 2011, 2013, 2015 The University of Utah
4 // All rights reserved.
5 //
6 // This file is part of `csmith', a random generator of C programs.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 //   * Redistributions of source code must retain the above copyright notice,
12 //     this list of conditions and the following disclaimer.
13 //
14 //   * Redistributions in binary form must reproduce the above copyright
15 //     notice, this list of conditions and the following disclaimer in the
16 //     documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 
30 //
31 // This file was derived from a random program generator written by Bryan
32 // Turner.  The attributions in that file was:
33 //
34 // Random Program Generator
35 // Bryan Turner (bryan.turner@pobox.com)
36 // July, 2005
37 //
38 
39 #ifndef EXPRESSION_H
40 #define EXPRESSION_H
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #include <ostream>
45 #include "CGContext.h"
46 #include "CVQualifiers.h"
47 #include "ProbabilityTable.h"
48 #include <vector>
49 #include <string>
50 using namespace std;
51 
52 class CGContext;
53 class Type;
54 class FunctionInvocation;
55 class FunctionInvocationUser;
56 class ExpressionVariable;
57 class CVQualifiers;
58 
59 #if 0
60 /*
61  *
62  */
63 enum eBinaryOp
64 {
65 	eAdd,
66 	eSub
67 };
68 #define MAX_BINARY_OPS ((eBinaryOp) (eSub+1))
69 #endif // 0
70 
71 /*
72  *
73  */
74 enum eTermType
75 {
76 	eConstant,
77 	eVariable,
78 	// eUnaryExpr,
79 	// eBinaryExpr,
80 	eFunction,
81 	eAssignment,
82 	eCommaExpr,
83 	eLhs
84 };
85 #define MAX_TERM_TYPES ((eTermType) (eCommaExpr+1))
86 
87 template <class Key, class Value>
88 class ProbabilityTable;
89 
90 /*
91  *
92  */
93 class Expression
94 {
95 public:
96 	// Factory method.
97 	static Expression *make_random(CGContext &cg_context, const Type* type, const CVQualifiers* qfer=0, bool no_func = false, bool no_const = false, enum eTermType tt=MAX_TERM_TYPES);
98 
99 	static Expression *make_random_param(CGContext &cg_context, const Type* type, const CVQualifiers* qfer, enum eTermType tt=MAX_TERM_TYPES);
100 
101 	static void InitProbabilityTables();
102 
103 	Expression(eTermType e);
104 
105 	Expression(const Expression &expr);
106 
107 	virtual ~Expression(void);
108 
109 	virtual Expression *clone() const = 0;
110 
111 	virtual const Type &get_type(void) const = 0;
112 
113 	virtual CVQualifiers get_qualifiers(void) const = 0;
114 
115 	virtual void get_eval_to_subexps(vector<const Expression*>& subs) const = 0;
116 
get_called_funcs(std::vector<const FunctionInvocationUser * > &)117 	virtual void get_called_funcs(std::vector<const FunctionInvocationUser*>& /*funcs*/ ) const {};
118 
get_invoke(void)119 	virtual const FunctionInvocation* get_invoke(void) const {return NULL;};
120 
visit_facts(vector<const Fact * > &,CGContext &)121 	virtual bool visit_facts(vector<const Fact*>& /*inputs*/, CGContext& /*cg_context*/) const {return true;};
122 
123 	virtual std::vector<const ExpressionVariable*> get_dereferenced_ptrs(void) const;
124 	virtual void get_referenced_ptrs(std::vector<const Variable*>& ptrs) const = 0;
125 
has_uncertain_call_recursive(void)126 	virtual bool has_uncertain_call_recursive(void) const {return false;}
127 
128 	virtual unsigned int get_complexity(void) const = 0;
less_than(int)129 	virtual bool less_than(int /*num*/) const { return false;}
not_equals(int)130 	virtual bool not_equals(int /*num*/) const { return false;}
equals(int)131 	virtual bool equals(int /*num*/) const { return false;}
is_0_or_1(void)132 	virtual bool is_0_or_1(void) const { return false;}
133 
use_var(const Variable *)134 	virtual bool use_var(const Variable* /* v */) const { return false;}
135 
136 	virtual void Output(std::ostream &) const = 0;
137 	virtual void indented_output(std::ostream &out, int indent) const;
138 
139 #if 0
140 	void OutputBinaryOp(std::ostream &) const;
141 #endif
142 
143 	unsigned int func_count(void) const;
144 
145 	std::string to_string(void) const;
146 
147 	static void record_dereference_level(int level);
148 
compatible(const Expression *)149 	virtual bool compatible(const Expression *) const { return false;}
150 
compatible(const Variable *)151 	virtual bool compatible(const Variable *) const { return false;}
152 
153 	void check_and_set_cast(const Type* t);
154 	void output_cast(std::ostream& out) const;
155 
156 	enum eTermType term_type;
157 	int expr_id;
158 
159 	const Type* cast_type;
160 
161 private:
162 	static void InitExprProbabilityTable();
163 	static void InitParamProbabilityTable();
164 
165 	static DistributionTable exprTable_;
166 	static DistributionTable paramTable_;
167 };
168 
169 ///////////////////////////////////////////////////////////////////////////////
170 
171 #endif // EXPRESSION_H
172 
173 // Local Variables:
174 // c-basic-offset: 4
175 // tab-width: 4
176 // End:
177 
178 // End of file.
179