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, 2013, 2014 Henrik Tidefelt
21 #include "Shapes_Ast_decls.h"
22 #include "Shapes_Kernel_decls.h"
33 Ast::SourceLocation loc_
; /* Note: Not a const reference, as this is part of the AST. */
36 /* Function formals play two different roles: they allow named arguments in function application, and they define the identifiers to which
37 * arguments are bound for access from the function body. In the first case, the formal is mapped to a simple identifier, while in the latter
38 * the formal is mapped to a placed identifier. (Using placed identifiers for naming arguments in a function application would just be
39 * inconvenient.) Therefore, we need two different maps that are just different ways of expressing the same order.
41 Ast::IdentifierTree
* argumentIdentifiers_
;
42 std::map
< const char *, size_t, charPtrLess
> * argumentOrder_
;
43 std::vector
< Ast::Expression
* > defaultExprs_
; /* The sink does not have a default expression at all. */
44 Ast::PlacedIdentifier
* sink_
; /* If null, there is no sink. */
45 std::vector
< bool > forcePos_
;
46 Ast::IdentifierTree
* stateIdentifiers_
;
47 std::map
< const char *, size_t, charPtrLess
> * stateOrder_
;
49 Formals( size_t numberOfDummyDefaultExprs
);
51 void setLoc( const Ast::SourceLocation
& loc
);
52 bool appendArgumentFormal( const Ast::PlacedIdentifier
& id
);
53 bool appendStateFormal( const Ast::PlacedIdentifier
& id
);
55 void push_exprs( Ast::ArgListExprs
* args
) const;
56 Kernel::EvaluatedFormals
* newEvaluatedFormals( Kernel::Arguments
& args
) const;
57 Kernel::EvaluatedFormals
* newEvaluatedFormals( Kernel::Arguments
& args
, size_t * pos
) const; /* values are taken at *pos, and *pos is incremented accordingly */
59 std::vector
< bool > * newArgListForcePos( const Ast::ArgListExprs
* argList
) const;
60 std::vector
< bool > * newArgListForcePos( const Ast::ArgListExprs
* argList
, const Kernel::Arguments
& curryArgs
) const;
62 bool hasSink( ) const { return sink_
!= 0; }
64 const Ast::SourceLocation
& loc( ) const;
69 std::vector
< bool > * forcePos_
;
70 bool forceAll_
; /* if forcePos == 0, then either all or none are forced according to this variable */
72 const Ast::ArgListExprs
* argList_
;
73 Kernel::PassedEnv env_
;
74 Kernel::PassedDyn dyn_
;
75 Kernel::ContRef cont_
;
77 CallContInfo( const Ast::ArgListExprs
* argList
, const Kernel::EvalState
& evalState
, std::vector
< bool > * forcePos
);
78 CallContInfo( const Ast::ArgListExprs
* argList
, const Kernel::EvalState
& evalState
, bool forceAll
);
81 bool force( const size_t & pos
) const;
82 bool isSelective( ) const;
83 bool forceNone( ) const;
84 bool forceAll( ) const;
85 void gcMark( Kernel::GCMarkedSet
& marked
);
96 bool firstOrderedStateOwner_
;
99 std::list
< Ast::Expression
* > * orderedExprs_
;
100 std::map
< const char *, Ast::Expression
*, charPtrLess
> * namedExprs_
;
101 std::list
< Ast::StateReference
* > * orderedStates_
;
102 std::map
< const char *, Ast::StateReference
*, charPtrLess
> * namedStates_
;
103 StateIDSet
* freeStates_
; /* Set of free states referenced by the expressions. */
108 std::list
< Ast::Expression
* >::const_reverse_iterator i1_
;
109 std::map
< const char *, Ast::Expression
*, charPtrLess
>::const_iterator i2_
;
112 ConstIterator( const ConstIterator
& orig
);
113 ConstIterator( std::list
< Ast::Expression
* >::const_reverse_iterator i1
, std::map
< const char *, Ast::Expression
*, charPtrLess
>::const_iterator i2
, const size_t & index
);
116 ArgListExprs( bool exprOwner
);
117 ArgListExprs( bool exprOwner
, bool firstOrderedStateOwner
);
118 ArgListExprs( std::list
< Ast::Expression
* > * orderedExprs
, std::map
< const char *, Ast::Expression
*, charPtrLess
> * namedExprs
,
119 std::list
< Ast::StateReference
* > * orderedStates
, std::map
< const char *, Ast::StateReference
*, charPtrLess
> * namedStates
);
120 ArgListExprs( size_t numberOfOrderedDummyExprs
);
122 void analyze( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
124 ConstIterator
begin( ) const;
126 void evaluate( const RefCountPtr
< const Kernel::CallContInfo
> & info
, const ArgListExprs::ConstIterator
& pos
, const RefCountPtr
< const Lang::SingleList
> & vals
, Kernel::EvalState
* evalState
) const;
127 void evaluate_Structure( const RefCountPtr
< const Kernel::CallContInfo
> & info
, size_t pos
, const RefCountPtr
< const Lang::SingleList
> & values
, Kernel::EvalState
* evalState
) const;
128 void bind( Kernel::Arguments
* dst
, RefCountPtr
< const Lang::SingleList
> vals
, Kernel::PassedEnv env
, Kernel::PassedDyn dyn
) const;
130 Kernel::VariableHandle
findNamed( RefCountPtr
< const Lang::SingleList
> vals
, const char * name
) const;
131 Kernel::VariableHandle
getOrdered( RefCountPtr
< const Lang::SingleList
> vals
, size_t pos
) const;
134 class FunctionFunction
: public Lang::Function
136 Ast::SourceLocation loc_
; /* Note: Not a const reference. */
137 const Kernel::Formals
* formals_
;
138 Ast::Expression
* body_
;
139 Ast::FunctionMode functionMode_
;
141 FunctionFunction( const Ast::SourceLocation
& loc
, const Kernel::Formals
* formals
, Ast::Expression
* body
, const Ast::FunctionMode
& functionMode
);
142 virtual ~FunctionFunction( );
144 void push_exprs( Ast::ArgListExprs
* args
) const;
146 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
147 virtual void call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const;
149 virtual void gcMark( Kernel::GCMarkedSet
& marked
){ };
150 virtual bool isTransforming( ) const { return false; }
153 class CallExpr
: public Expression
156 RefCountPtr
< const Lang::Function
> constFun_
;
157 Ast::StateReference
* mutatorSelf_
;
159 Ast::Expression
* funExpr_
;
160 Ast::ArgListExprs
* argList_
;
162 CallExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* funExpr
, Ast::ArgListExprs
* argList
, bool curry
= false );
163 CallExpr( const Ast::SourceLocation
& loc
, const RefCountPtr
< const Lang::Function
> & constFun
, Ast::ArgListExprs
* argList
, bool curry
= false );
164 virtual ~CallExpr( );
165 void setMutatorSelf( Ast::StateReference
* mutatorSelf
);
166 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
167 virtual void eval( Kernel::EvalState
* evalState
) const;
170 class UnionExpr
: public Expression
173 Ast::ArgListExprs
* argList_
;
175 UnionExpr( const Ast::SourceLocation
& loc
, Ast::ArgListExprs
* argList
);
176 virtual ~UnionExpr( );
177 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
178 virtual void eval( Kernel::EvalState
* evalState
) const;
181 class CallSplitExpr
: public Expression
184 Ast::Expression
* funExpr_
;
185 Ast::Expression
* argList_
;
188 CallSplitExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* funExpr
, Ast::Expression
* argList
, bool curry
= false );
189 virtual ~CallSplitExpr( );
190 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
191 virtual void eval( Kernel::EvalState
* evalState
) const;
194 class DummyExpression
: public Expression
198 DummyExpression( const Ast::SourceLocation
& loc
);
199 virtual ~DummyExpression( );
200 virtual void analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
);
201 virtual void eval( Kernel::EvalState
* evalState
) const;