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_Kernel_decls.h"
30 class SourceLocationMark
: public Node
33 SourceLocationMark( const Ast::SourceLocation
& loc
);
34 virtual ~SourceLocationMark( );
36 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
40 class CodeBracket
: public Expression
43 typedef std::map
< const char *, size_t, charPtrLess
> MapType
;
45 std::list
< Ast::Node
* > * nodes_
;
46 Ast::CodeBracket
* extends_
;
48 MapType
* argumentOrder_
;
49 MapType
* dynamicMap_
;
50 MapType
* stateOrder_
;
52 CodeBracket( const Ast::SourceLocation
& loc
, std::list
< Ast::Node
* > * nodes
, Ast::CodeBracket
* extends
= 0 );
53 virtual ~CodeBracket( );
55 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
56 virtual void eval( Kernel::EvalState
* evalState
) const;
58 Kernel::Environment
* newEnvironment( Kernel::Environment
* parent
, const char * debugName
= "Bracket" ) const;
59 void eval( Kernel::EvalState
* evalState
, Kernel::Environment
* extendsEnv
) const;
61 void evalAt( const RefCountPtr
< const Kernel::CodeBracketContInfo
> & info
, const std::list
< Ast::Node
* >::const_iterator
& i
, Kernel::EvalState
* evalState
) const;
63 void setDynamicMap( MapType
* dynamicMap
);
70 class CodeBracketContInfo
73 const Ast::CodeBracket
* bracketExpr_
;
74 Kernel::PassedEnv env_
;
75 Kernel::PassedDyn dyn_
;
76 Kernel::ContRef cont_
;
78 CodeBracketContInfo( const Ast::CodeBracket
* bracketExpr
, const Kernel::EvalState
& evalState
);
79 ~CodeBracketContInfo( );
81 void gcMark( Kernel::GCMarkedSet
& marked
);
84 class CodeBracketContinuation
: public Kernel::Continuation
86 RefCountPtr
< const Kernel::CodeBracketContInfo
> info_
;
87 std::list
< Ast::Node
* >::const_iterator pos_
;
89 CodeBracketContinuation( const Ast::SourceLocation
& _traceLoc
, const RefCountPtr
< const Kernel::CodeBracketContInfo
> & _info
, const std::list
< Ast::Node
* >::const_iterator
& _pos
);
90 virtual ~CodeBracketContinuation( );
91 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
92 virtual Kernel::ContRef
up( ) const;
93 virtual RefCountPtr
< const char > description( ) const;
94 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
98 class DynamicBindingContinuation
: public Kernel::Continuation
100 Kernel::PassedEnv env_
;
101 Kernel::Environment::LexicalKey key_
;
102 const Ast::DynamicBindingExpression
* bindingExpr_
;
103 Kernel::ContRef cont_
;
105 DynamicBindingContinuation( const Ast::SourceLocation
& traceLoc
, const Kernel::PassedEnv
& env
, const Kernel::Environment::LexicalKey
& key
, const Ast::DynamicBindingExpression
* bindingExpr
, const Kernel::ContRef
& cont
);
106 virtual ~DynamicBindingContinuation( );
107 virtual void takeHandle( Kernel::VariableHandle val
, Kernel::EvalState
* evalState
, bool dummy
) const;
108 virtual Kernel::ContRef
up( ) const;
109 virtual RefCountPtr
< const char > description( ) const;
110 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
113 class WithDynamicContinuation
: public Kernel::Continuation
115 Ast::Expression
* expr_
;
116 Kernel::PassedEnv env_
;
117 Kernel::PassedDyn dyn_
;
118 Kernel::ContRef cont_
;
120 WithDynamicContinuation( const Ast::SourceLocation
& traceLoc
, Ast::Expression
* expr
, const Kernel::EvalState
& evalState
);
121 virtual ~WithDynamicContinuation( );
122 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
123 virtual Kernel::ContRef
up( ) const;
124 virtual RefCountPtr
< const char > description( ) const;
125 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
128 class DynamicVariableDeclContinuation
: public Kernel::Continuation
130 const Ast::DynamicVariableDecl
* declExpr_
;
131 Kernel::PassedEnv env_
;
132 Kernel::PassedDyn dyn_
;
133 Kernel::ContRef cont_
;
135 DynamicVariableDeclContinuation( const Ast::SourceLocation
& traceLoc
, const Ast::DynamicVariableDecl
* declExpr
, Kernel::EvalState
& evalState
);
136 virtual ~DynamicVariableDeclContinuation( );
137 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
138 virtual Kernel::ContRef
up( ) const;
139 virtual RefCountPtr
< const char > description( ) const;
140 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
143 class AssertStructureContinuation
: public Kernel::Continuation
145 Kernel::ContRef cont_
;
147 AssertStructureContinuation( const Kernel::ContRef
& cont
, const Ast::SourceLocation
& traceLoc
);
148 ~AssertStructureContinuation( );
149 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
150 virtual Kernel::ContRef
up( ) const;
151 virtual RefCountPtr
< const char > description( ) const;
152 virtual void gcMark( Kernel::GCMarkedSet
& marked
);
155 } // End of namespace Kernel.
159 class LexiographicVariable
: public Expression
162 mutable Kernel::Environment::LexicalKey
** idKey_
;
164 LexiographicVariable( const Ast::SourceLocation
& loc
, const char * id
, Kernel::Environment::LexicalKey
** idKey
);
165 virtual ~LexiographicVariable( );
166 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
167 virtual void eval( Kernel::EvalState
* evalState
) const;
170 class EvalOutsideExpr
: public Expression
172 Ast::Expression
* expr_
;
174 EvalOutsideExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* expr
);
175 virtual ~EvalOutsideExpr( );
176 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
177 virtual void eval( Kernel::EvalState
* evalState
) const;
180 class DefineVariable
: public BindNode
182 Ast::Expression
* expr_
;
183 mutable size_t ** idPos_
;
185 DefineVariable( const Ast::SourceLocation
& idLoc
, const char * id
, Ast::Expression
* expr
, size_t ** idPos
);
186 virtual ~DefineVariable( );
187 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
188 virtual void evalHelper( Kernel::EvalState
* evalState
) const;
191 class LexicalVariableLocationExpr
: public Expression
194 typedef enum { INDEX
, DEPTH
} Kind
;
198 RefCountPtr
< const Lang::Value
> value_
;
200 LexicalVariableLocationExpr( const Ast::SourceLocation
& idLoc
, const char * id
, Kind kind
);
201 virtual ~LexicalVariableLocationExpr( );
202 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
203 virtual void eval( Kernel::EvalState
* evalState
) const;
206 class LexicalVariableNameExpr
: public Expression
208 RefCountPtr
< const Lang::Value
> value_
;
210 LexicalVariableNameExpr( const Ast::SourceLocation
& loc
);
211 virtual ~LexicalVariableNameExpr( );
212 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
213 virtual void eval( Kernel::EvalState
* evalState
) const;
216 class StructSplitReference
: public Expression
218 Ast::SourceLocation structLoc_
;
219 size_t ** structPos_
;
220 const char * fieldId_
;
222 Ast::Expression
* defaultExpr_
;
224 StructSplitReference( Ast::SourceLocation fieldLoc
, const char * fieldId
, Ast::Expression
* defaultExpr
);
225 StructSplitReference( Ast::SourceLocation fieldLoc
, size_t fieldPos
, Ast::Expression
* defaultExpr
);
226 virtual ~StructSplitReference( );
227 void setStruct( Ast::SourceLocation structLoc
, size_t ** structPos
);
228 bool isOrdered( ) const { return fieldId_
== 0; }
229 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
230 virtual void eval( Kernel::EvalState
* evalState
) const;
233 class StructSplitSink
: public Expression
235 Ast::SourceLocation structLoc_
;
236 size_t ** structPos_
;
237 size_t consumedArguments_
;
240 virtual ~StructSplitSink( );
241 void setStruct( Ast::SourceLocation structLoc
, size_t ** structPos
, size_t consumedArguments
);
242 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
243 virtual void eval( Kernel::EvalState
* evalState
) const;
246 class AssertNoSinkNeeded
: public Assertion
248 size_t orderedCount_
;
249 Ast::SourceLocation structLoc_
;
250 size_t ** structPos_
;
252 AssertNoSinkNeeded( const Ast::SourceLocation
& loc
, size_t orderedCount
, Ast::SourceLocation structLoc
, size_t ** structPos
);
253 virtual ~AssertNoSinkNeeded( );
254 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
255 virtual void eval( Kernel::EvalState
* evalState
) const;
258 class SplitDefineVariables
260 static size_t splitVarCount
;
261 static PtrOwner_back_Access
< std::list
< const char * > > mem
;
262 const char * splitVarId_
;
264 std::list
< std::pair
< Ast::DefineVariable
*, Ast::StructSplitReference
* > > exprs_
;
265 Ast::DefineVariable
* sinkDefine_
;
266 Ast::StructSplitSink
* sinkExpr_
;
269 SplitDefineVariables( );
270 const char * newSplitVarId( ) const;
273 class StateReference
: public Node
276 Node
* someParent_
; // The inherited parent_ does not make sense for reused nodes. Use this instead.
277 Ast::AnalysisEnvironment
* someAnalysisEnv_
; // The inherited analysisEnv_ does not make sense for reused nodes. Use this instead.
279 StateReference( const Ast::SourceLocation
& loc
);
280 virtual ~StateReference( );
281 virtual Kernel::StateHandle
getHandle( Kernel::PassedEnv env
, Kernel::PassedDyn dyn
) const = 0;
284 class LexiographicState
: public StateReference
287 mutable Kernel::Environment::LexicalKey
** idKey_
;
289 LexiographicState( const Ast::SourceLocation
& loc
, const char * id
, Kernel::Environment::LexicalKey
** idKey
);
290 virtual ~LexiographicState( );
291 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
292 Kernel::StateHandle
getHandle( Kernel::PassedEnv env
, Kernel::PassedDyn dyn
) const;
295 class DynamicState
: public Ast::StateReference
298 mutable Kernel::Environment::LexicalKey
** idKey_
;
300 DynamicState( const Ast::SourceLocation
& loc
, const char * id
);
301 virtual ~DynamicState( );
302 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
303 Kernel::StateHandle
getHandle( Kernel::PassedEnv env
, Kernel::PassedDyn dyn
) const;
306 class IntroduceState
: public BindNode
308 Ast::Expression
* expr_
;
309 mutable size_t ** idPos_
;
310 Ast::StateID stateID_
;
312 IntroduceState( const Ast::SourceLocation
& idLoc
, const char * id
, Ast::Expression
* expr
, size_t ** idPos
);
313 virtual ~IntroduceState( );
314 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
315 virtual void evalHelper( Kernel::EvalState
* evalState
) const;
316 Ast::StateID
getStateID( ) const { return stateID_
; } /* Make sure analyze_impl has been called first! */
319 class Insertion
: public Expression
321 Ast::StateReference
* stateRef_
;
322 Ast::Expression
* expr_
;
324 Insertion( Ast::StateReference
* stateRef
, Ast::Expression
* expr
);
325 virtual ~Insertion( );
326 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
327 virtual void eval( Kernel::EvalState
* evalState
) const;
330 class Freeze
: public Expression
333 mutable size_t ** idPos_
;
335 Freeze( const Ast::SourceLocation
& idLoc
, const char * id
, size_t ** idPos
);
337 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
338 virtual void eval( Kernel::EvalState
* evalState
) const;
341 class Peek
: public Expression
343 Ast::StateReference
* stateRef_
;
345 Peek( const Ast::SourceLocation
& idLoc
, Ast::StateReference
* stateRef
);
347 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
348 virtual void eval( Kernel::EvalState
* evalState
) const;
351 class EvalSymbolFunction
: public Lang::Function
353 Ast::SourceLocation loc_
;
354 Ast::Expression
* expr_
;
355 Ast::AnalysisEnvironment
* analysisEnv_
;
357 EvalSymbolFunction( const Ast::SourceLocation
& loc
, Ast::Expression
* expr
);
358 virtual ~EvalSymbolFunction( );
359 void push_exprs( Ast::ArgListExprs
* args
) const;
360 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
361 virtual void call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const;
362 virtual void gcMark( Kernel::GCMarkedSet
& marked
){ };
363 virtual bool isTransforming( ) const { return false; }
366 class MemberReferenceFunction
: public Lang::Function
368 Ast::SourceLocation loc_
;
369 Ast::Expression
* variable_
;
370 const char * fieldID_
;
372 MemberReferenceFunction( const Ast::SourceLocation
& loc
, Ast::Expression
* variable
, const char * fieldID
);
373 virtual ~MemberReferenceFunction( );
374 void push_exprs( Ast::ArgListExprs
* args
) const;
375 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
376 virtual void call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const;
377 virtual void gcMark( Kernel::GCMarkedSet
& marked
){ };
378 virtual bool isTransforming( ) const { return false; }
381 class MutatorReference
: public Ast::Expression
383 Ast::SourceLocation mutatorLoc_
;
384 Ast::StateReference
* state_
;
385 const char * mutatorID_
;
387 MutatorReference( const Ast::SourceLocation
& mutatorLoc
, Ast::StateReference
* state
, const char * mutatorID
);
388 virtual ~MutatorReference( );
389 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
390 virtual void eval( Kernel::EvalState
* evalState
) const;
393 class SpecialLength
: public Expression
398 SpecialLength( const Ast::SourceLocation
& loc
, double val
, int sort
);
399 virtual ~SpecialLength( );
400 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
401 virtual void eval( Kernel::EvalState
* evalState
) const;
404 class DynamicVariable
: public Ast::Expression
407 mutable Kernel::Environment::LexicalKey
** idKey_
;
409 DynamicVariable( const Ast::SourceLocation
& loc
, const char * id
);
410 virtual ~DynamicVariable( );
411 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
412 virtual void eval( Kernel::EvalState
* evalState
) const;
415 class DynamicBindingExpression
: public Ast::Expression
417 Ast::SourceLocation idLoc_
;
419 Ast::Expression
* expr_
;
420 mutable Kernel::Environment::LexicalKey
** idKey_
;
422 DynamicBindingExpression( const Ast::SourceLocation
& idLoc
, const char * id
, Ast::Expression
* expr
, Kernel::Environment::LexicalKey
** idKey
);
423 virtual ~DynamicBindingExpression( );
424 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
425 virtual void eval( Kernel::EvalState
* evalState
) const;
426 const Ast::SourceLocation
& idLoc( ) const { return idLoc_
; }
427 const Ast::SourceLocation
& exprLoc( ) const { return expr_
->loc( ); }
428 const char * id( ) const { return id_
; }
431 class DynamicStateBindingExpression
: public Ast::Expression
433 Ast::SourceLocation dstLoc_
;
435 mutable Kernel::Environment::LexicalKey
** dstIdKey_
;
436 Ast::StateReference
* src_
;
438 DynamicStateBindingExpression( const Ast::SourceLocation
& loc
, const Ast::SourceLocation
& dstLoc
, const char * dstId
, Ast::StateReference
* src
);
439 virtual ~DynamicStateBindingExpression( );
440 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
441 virtual void eval( Kernel::EvalState
* evalState
) const;
444 class WithDynamicExpr
: public Ast::Expression
446 Ast::Expression
* bindings_
;
447 Ast::Expression
* expr_
;
449 WithDynamicExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* bindings
, Ast::Expression
* expr
);
450 virtual ~WithDynamicExpr( );
451 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
452 virtual void eval( Kernel::EvalState
* evalState
) const;
455 class DynamicVariableDecl
: public BindNode
457 mutable size_t ** idPos_
;
458 Ast::Expression
* impl_
;
460 DynamicVariableDecl( const Ast::SourceLocation
& loc
, const Ast::SourceLocation
& idLoc
, const char * id
, Ast::Expression
* filterExpr
, Ast::Expression
* defaultExpr
);
461 virtual ~DynamicVariableDecl( );
462 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
463 virtual void evalHelper( Kernel::EvalState
* evalState
) const;
466 class DynamicVariableDeclFunction
: public Lang::Function
469 Ast::Expression
* filterExpr_
;
470 Ast::Expression
* defaultExpr_
;
473 DynamicVariableDeclFunction( const char * id
, Ast::Expression
* filterExpr
, Ast::Expression
* defaultExpr
, size_t ** idPos
);
474 virtual ~DynamicVariableDeclFunction( );
475 void push_exprs( Ast::ArgListExprs
* args
) const;
476 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
477 virtual void call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const;
478 virtual void gcMark( Kernel::GCMarkedSet
& marked
){ };
479 virtual bool isTransforming( ) const { return false; }
482 class DynamicStateDecl
: public BindNode
484 const char * defaultStateID_
;
485 mutable size_t ** idPos_
;
486 Ast::StateReference
* defaultState_
;
488 DynamicStateDecl( const Ast::SourceLocation
& loc
, const Ast::SourceLocation
& idLoc
, const char * id
, Ast::StateReference
* defaultState
, size_t ** idPos
);
489 virtual ~DynamicStateDecl( );
490 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
491 virtual void evalHelper( Kernel::EvalState
* evalState
) const;
494 class DynamicExpression
: public Ast::Expression
496 Ast::Expression
* expr_
;
498 DynamicExpression( const Ast::SourceLocation
& loc
, Ast::Expression
* expr
);
499 virtual ~DynamicExpression( );
500 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
501 virtual void eval( Kernel::EvalState
* evalState
) const;
504 class LexiographicType
: public Expression
507 mutable Kernel::Environment::LexicalKey
** idKey_
;
509 LexiographicType( const Ast::SourceLocation
& loc
, const char * id
, Kernel::Environment::LexicalKey
** idKey
);
510 virtual ~LexiographicType( );
511 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
512 virtual void eval( Kernel::EvalState
* evalState
) const;