1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
21 #include "Shapes_Ast_decls.h"
22 #include "Shapes_Kernel_decls.h"
33 Ast::SourceLocation loc_
;
36 std::map
< const char *, size_t, charPtrLess
> * argumentOrder_
;
37 std::vector
< Ast::Expression
* > defaultExprs_
; // The sink does not have a default expression at all.
38 const char * sink_
; // If null, there is no sink.
39 std::vector
< bool > forcePos_
;
40 std::map
< const char *, size_t, charPtrLess
> * stateOrder_
;
42 Formals( size_t numberOfDummyDefaultExprs
);
44 void setLoc( Ast::SourceLocation loc
);
46 void push_exprs( Ast::ArgListExprs
* args
) const;
47 Kernel::EvaluatedFormals
* newEvaluatedFormals( Kernel::Arguments
& args
) const;
48 Kernel::EvaluatedFormals
* newEvaluatedFormals( Kernel::Arguments
& args
, size_t * pos
) const; // values are taken at *pos, and *pos is incremented accordingly
50 std::vector
< bool > * newArgListForcePos( const Ast::ArgListExprs
* argList
) const;
51 std::vector
< bool > * newArgListForcePos( const Ast::ArgListExprs
* argList
, const Kernel::Arguments
& curryArgs
) const;
53 bool hasSink( ) const { return sink_
!= 0; }
55 const Ast::SourceLocation
& loc( ) const;
60 std::vector
< bool > * forcePos_
;
61 bool forceAll_
; /* if forcePos == 0, then either all or none are forced according to this variable */
63 const Ast::ArgListExprs
* argList_
;
64 Kernel::PassedEnv env_
;
65 Kernel::PassedDyn dyn_
;
66 Kernel::ContRef cont_
;
68 CallContInfo( const Ast::ArgListExprs
* argList
, const Kernel::EvalState
& evalState
, std::vector
< bool > * forcePos
);
69 CallContInfo( const Ast::ArgListExprs
* argList
, const Kernel::EvalState
& evalState
, bool forceAll
);
72 bool force( const size_t & pos
) const;
73 bool isSelective( ) const;
74 void gcMark( Kernel::GCMarkedSet
& marked
);
87 std::list
< Ast::Expression
* > * orderedExprs_
;
88 std::map
< const char *, Ast::Expression
*, charPtrLess
> * namedExprs_
;
89 std::list
< Ast::StateReference
* > * orderedStates_
;
90 std::map
< const char *, Ast::StateReference
*, charPtrLess
> * namedStates_
;
91 StateIDSet
* freeStates_
; /* Set of free states referenced by the expressions. */
96 std::list
< Ast::Expression
* >::const_reverse_iterator i1_
;
97 std::map
< const char *, Ast::Expression
*, charPtrLess
>::const_iterator i2_
;
100 ConstIterator( const ConstIterator
& orig
);
101 ConstIterator( std::list
< Ast::Expression
* >::const_reverse_iterator i1
, std::map
< const char *, Ast::Expression
*, charPtrLess
>::const_iterator i2
, const size_t & index
);
104 ArgListExprs( bool exprOwner
);
105 ArgListExprs( std::list
< Ast::Expression
* > * orderedExprs
, std::map
< const char *, Ast::Expression
*, charPtrLess
> * namedExprs
,
106 std::list
< Ast::StateReference
* > * orderedStates
, std::map
< const char *, Ast::StateReference
*, charPtrLess
> * namedStates
);
107 ArgListExprs( size_t numberOfOrderedDummyExprs
);
109 void analyze( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
111 ConstIterator
begin( ) const;
113 void evaluate( const RefCountPtr
< const Kernel::CallContInfo
> & info
, const ArgListExprs::ConstIterator
& pos
, const RefCountPtr
< const Lang::SingleList
> & vals
, Kernel::EvalState
* evalState
) const;
114 void bind( Kernel::Arguments
* dst
, RefCountPtr
< const Lang::SingleList
> vals
, Kernel::PassedEnv env
, Kernel::PassedDyn dyn
) const;
116 Kernel::VariableHandle
findNamed( RefCountPtr
< const Lang::SingleList
> vals
, const char * name
) const;
117 Kernel::VariableHandle
getOrdered( RefCountPtr
< const Lang::SingleList
> vals
, size_t pos
) const;
120 class FunctionFunction
: public Lang::Function
122 Ast::SourceLocation loc_
;
123 const Kernel::Formals
* formals_
;
124 Ast::Expression
* body_
;
125 Ast::FunctionMode functionMode_
;
127 FunctionFunction( const Ast::SourceLocation
& loc
, const Kernel::Formals
* formals
, Ast::Expression
* body
, const Ast::FunctionMode
& functionMode
);
128 virtual ~FunctionFunction( );
130 void push_exprs( Ast::ArgListExprs
* args
) const;
132 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
133 virtual void call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const;
135 virtual void gcMark( Kernel::GCMarkedSet
& marked
){ };
136 virtual bool isTransforming( ) const { return false; }
144 class CallCont_1
: public Kernel::Continuation
146 const Ast::ArgListExprs
* argList_
;
148 Kernel::StateHandle mutatorSelf_
;
149 Kernel::PassedEnv env_
;
150 Kernel::PassedDyn dyn_
;
151 Kernel::ContRef cont_
;
152 const Ast::SourceLocation
& callLoc_
;
154 CallCont_1( const Ast::SourceLocation
& traceLoc
, const Ast::ArgListExprs
* argList
, bool curry
, Kernel::StateHandle mutatorSelf
, const Kernel::EvalState
& evalState
, const Ast::SourceLocation
& callLoc
);
155 virtual ~CallCont_1( );
156 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
157 virtual Kernel::ContRef
up( ) const;
158 virtual RefCountPtr
< const char > description( ) const;
159 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
162 class CallCont_last
: public Kernel::Continuation
164 RefCountPtr
< const Lang::Function
> fun_
;
165 const Ast::ArgListExprs
* argList_
;
167 Kernel::StateHandle mutatorSelf_
;
168 Kernel::PassedEnv env_
;
169 Kernel::PassedDyn dyn_
;
170 Kernel::ContRef cont_
;
172 CallCont_last( const RefCountPtr
< const Lang::Function
> & fun
, const Ast::ArgListExprs
* argList
, bool curry
, Kernel::StateHandle mutatorSelf
, const Kernel::PassedEnv
& env
, const Kernel::PassedDyn
& dyn
, const Kernel::ContRef
& cont
, const Ast::SourceLocation
& callLoc
);
173 virtual ~CallCont_last( );
174 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & valsUntyped
, Kernel::EvalState
* evalState
, bool dummy
) const;
175 virtual Kernel::ContRef
up( ) const;
176 virtual RefCountPtr
< const char > description( ) const;
177 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
180 class CallCont_n
: public Kernel::Continuation
182 RefCountPtr
< const Kernel::CallContInfo
> info_
;
183 Ast::ArgListExprs::ConstIterator pos_
;
184 RefCountPtr
< const Lang::SingleList
> vals_
;
186 CallCont_n( const Ast::SourceLocation
& traceLoc
, const RefCountPtr
< const Kernel::CallContInfo
> & info
, const Ast::ArgListExprs::ConstIterator
& pos
, RefCountPtr
< const Lang::SingleList
> vals
);
187 virtual ~CallCont_n( );
188 virtual void takeHandle( Kernel::VariableHandle val
, Kernel::EvalState
* evalState
, bool dummy
) const;
189 virtual Kernel::ContRef
up( ) const;
190 virtual RefCountPtr
< const char > description( ) const;
191 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
194 class UnionCont_last
: public Kernel::Continuation
196 const Ast::ArgListExprs
* argList_
;
197 Kernel::ContRef cont_
;
199 UnionCont_last( const Ast::ArgListExprs
* argList
, const Kernel::ContRef
& cont
, const Ast::SourceLocation
& callLoc
);
200 virtual ~UnionCont_last( );
201 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & valsUntyped
, Kernel::EvalState
* evalState
, bool dummy
) const;
202 virtual Kernel::ContRef
up( ) const;
203 virtual RefCountPtr
< const char > description( ) const;
204 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
207 class SplitCont_1
: public Kernel::Continuation
209 Ast::Expression
* argList_
;
211 Kernel::PassedEnv env_
;
212 Kernel::PassedDyn dyn_
;
213 Kernel::ContRef cont_
;
214 const Ast::SourceLocation
& callLoc_
;
216 SplitCont_1( const Ast::SourceLocation
& traceLoc
, Ast::Expression
* argList
, bool curry
, const Kernel::EvalState
& evalState
, const Ast::SourceLocation
& callLoc
);
217 virtual ~SplitCont_1( );
218 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
219 virtual Kernel::ContRef
up( ) const;
220 virtual RefCountPtr
< const char > description( ) const;
221 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
224 class SplitCont_2
: public Kernel::Continuation
226 RefCountPtr
< const Lang::Function
> fun_
;
228 Kernel::PassedEnv env_
;
229 Kernel::PassedDyn dyn_
;
230 Kernel::ContRef cont_
;
232 SplitCont_2( const RefCountPtr
< const Lang::Function
> & fun
, bool curry
, const Kernel::PassedEnv
& env
, const Kernel::PassedDyn
& dyn
, const Kernel::ContRef
& cont
, const Ast::SourceLocation
& callLoc
);
233 virtual ~SplitCont_2( );
234 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & valsUntyped
, Kernel::EvalState
* evalState
, bool dummy
) const;
235 virtual Kernel::ContRef
up( ) const;
236 virtual RefCountPtr
< const char > description( ) const;
237 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
245 class CallExpr
: public Expression
248 RefCountPtr
< const Lang::Function
> constFun_
;
249 Ast::StateReference
* mutatorSelf_
;
251 Ast::Expression
* funExpr_
;
252 Ast::ArgListExprs
* argList_
;
254 CallExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* funExpr
, Ast::ArgListExprs
* argList
, bool curry
= false );
255 CallExpr( const Ast::SourceLocation
& loc
, const RefCountPtr
< const Lang::Function
> & constFun
, Ast::ArgListExprs
* argList
, bool curry
= false );
256 virtual ~CallExpr( );
257 void setMutatorSelf( Ast::StateReference
* mutatorSelf
);
258 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
259 virtual void eval( Kernel::EvalState
* evalState
) const;
262 class UnionExpr
: public Expression
265 Ast::ArgListExprs
* argList_
;
267 UnionExpr( const Ast::SourceLocation
& loc
, Ast::ArgListExprs
* argList
);
268 virtual ~UnionExpr( );
269 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
270 virtual void eval( Kernel::EvalState
* evalState
) const;
273 class CallSplitExpr
: public Expression
276 Ast::Expression
* funExpr_
;
277 Ast::Expression
* argList_
;
280 CallSplitExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* funExpr
, Ast::Expression
* argList
, bool curry
= false );
281 virtual ~CallSplitExpr( );
282 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
283 virtual void eval( Kernel::EvalState
* evalState
) const;
286 class DummyExpression
: public Expression
290 DummyExpression( const Ast::SourceLocation
& loc
);
291 virtual ~DummyExpression( );
292 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
293 virtual void eval( Kernel::EvalState
* evalState
) const;