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, 2009, 2014 Henrik Tidefelt
21 #include "Shapes_Ast_decls.h"
22 #include "Shapes_Kernel_decls.h"
25 #include "shapestypes.h"
26 #include "dynamicenvironment.h"
27 #include "sourcelocation.h"
42 Ast::Expression
* expr_
;
43 Kernel::PassedEnv env_
;
44 Kernel::PassedDyn dyn_
;
45 Kernel::ContRef cont_
;
47 EvalState( Ast::Expression
* expr
, const Kernel::PassedEnv
& env
, const Kernel::PassedDyn
& dyn
, const Kernel::ContRef
& cont
)
48 : expr_( expr
), env_( env
), dyn_( dyn
), cont_( cont
)
55 const Ast::SourceLocation
& traceLoc_
;
57 Continuation( const Ast::SourceLocation
& traceLoc
)
58 : traceLoc_( traceLoc
)
60 virtual ~Continuation( )
62 virtual void takeHandle( Kernel::VariableHandle val
, Kernel::EvalState
* evalState
, bool callingMyself
= false ) const;
63 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool callingMyself
= false ) const;
64 virtual void gcMark( Kernel::GCMarkedSet
& marked
) = 0;
65 virtual Kernel::ContRef
up( ) const = 0; // Returns a NullPtr when there is nothing to return.
66 virtual RefCountPtr
< const char > description( ) const = 0; // outer continuation before ourselves in the list, that is first push_front ourselves, then recurse.
67 const Ast::SourceLocation
& traceLoc( ) const;
68 void backTrace( std::ostream
& os
) const;
71 class ForcedStructureContinuation
: public Continuation
74 const char * continuationName_
;
76 ForcedStructureContinuation( const char * continuationName
, const Ast::SourceLocation
& traceLoc
);
77 virtual ~ForcedStructureContinuation( );
78 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const;
79 virtual void takeStructure( const RefCountPtr
< const Lang::Structure
> & structure
, Kernel::EvalState
* evalState
) const = 0;
81 static RefCountPtr
< const Lang::SingleList
> findUnforced( RefCountPtr
< const Lang::SingleList
> lst
);
92 typedef std::vector
< Ast::Node
* > ChildrenType
;
95 Ast::AnalysisEnvironment
* analysisEnv_
;
96 const Ast::SourceLocation loc_
; /* Note: Not a const reference. */
97 ChildrenType children_
;
99 Node( const Ast::SourceLocation
& loc
);
100 Node( const Ast::SourceLocation
& firstLoc
, const Ast::SourceLocation
& lastLoc
); /* Call SourceLocation two argument constructor on the fly. */
102 void analyze( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
103 /* The setParent and changeParent methods shall only be used when manipulating already analyzed expression,
104 * for example, when wrapping an already analyzed expression in Ast::Force.
106 void setParent( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
);
107 void changeParent( Ast::Node
* parent
);
108 const Ast::SourceLocation
& loc( ) const;
109 Ast::Node
* parent( );
110 ChildrenType
& children( );
111 Ast::AnalysisEnvironment
* getEnv( );
112 Ast::Expression
* findExpressionSameFile( const Ast::SourceLocation
& loc
);
114 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
) = 0;
117 class Expression
: public Node
120 /* The immediate_ flag means that the expression should not be delayed. This is not the same as forcing
121 * the expression (compare Ast::Force), which keeps forcing the result of the expression until a value
125 Kernel::BreakpointInfo
* breakpoint_
; /* Pointer is null if there is no breakpoint. */
127 Expression( const Ast::SourceLocation
& loc
);
128 Expression( const Ast::SourceLocation
& firstLoc
, const Ast::SourceLocation
& lastLoc
); /* Call SourceLocation two argument constructor on the fly. */
129 virtual ~Expression( );
130 virtual void eval( Kernel::EvalState
* evalState
) const = 0;
133 class BindNode
: public Node
136 const Ast::SourceLocation idLoc_
; /* Note: Not a const reference. */
137 const Ast::PlacedIdentifier
* id_
;
139 BindNode( const Ast::SourceLocation
& loc
, const Ast::SourceLocation
& idLoc
, const Ast::PlacedIdentifier
* id
);
140 BindNode( const Ast::SourceLocation
& firstLoc
, const Ast::SourceLocation
& lastLoc
, const Ast::SourceLocation
& idLoc
, const Ast::PlacedIdentifier
* id
); /* Call SourceLocation two argument constructor on the fly. */
141 virtual ~BindNode( );
142 const Ast::PlacedIdentifier
* id( ) const;
143 const Ast::SourceLocation
& idLoc( ) const;
144 virtual void initializeEnvironment( Kernel::PassedEnv env
, Kernel::PassedDyn dyn
) const = 0;
145 virtual void evalHelper( Kernel::EvalState
* evalState
) const = 0; /* Avoid the name <eval>, to make it easy to keep track of all places in the source where Expression::eval is invoked. */
148 class Assertion
: public Expression
151 Assertion( const Ast::SourceLocation
& loc
);
152 virtual ~Assertion( );
155 class ErrorExpression
: public Expression
158 ErrorExpression( const Ast::SourceLocation
& loc
);
159 virtual ~ErrorExpression( );
160 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
161 virtual void eval( Kernel::EvalState
* evalState
) const;