ExpressionVariable.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Translator ExpressionVariable.cpp */
40 /* */
41 /* Primitive Translator Expression Parsing Variable Reference Class */
42 /* */
43 /******************************************************************************/
44 #include <stdlib.h>
45 #include "RexxCore.h"
46 #include "StringClass.hpp"
47 #include "RexxActivation.hpp"
48 #include "RexxActivity.hpp"
50 #include "RexxVariable.hpp"
51 #include "ExpressionVariable.hpp"
52 
54  RexxString *variable_name, /* variable name to access */
55  size_t var_index) /* dictionary lookaside index */
56 /******************************************************************************/
57 /* Complete initialization of a variable object */
58 /******************************************************************************/
59 {
60  /* set the name value */
61  OrefSet(this, this->variableName, variable_name);
62  this->index = var_index; /* save the index */
63 }
64 
65 void RexxParseVariable::live(size_t liveMark)
66 /******************************************************************************/
67 /* Function: Normal garbage collection live marking */
68 /******************************************************************************/
69 {
71 }
72 
74 /******************************************************************************/
75 /* Function: Generalized object marking */
76 /******************************************************************************/
77 {
79 }
80 
82 /******************************************************************************/
83 /* Function: Flatten an object */
84 /******************************************************************************/
85 {
87 
88  flatten_reference(newThis->variableName, envelope);
89 
91 }
92 
94  RexxActivation *context, /* current activation context */
95  RexxExpressionStack *stack ) /* evaluation stack */
96 /******************************************************************************/
97 /* Function: Evaluate a REXX simple variable */
98 /******************************************************************************/
99 {
100  /* look up the name */
101  RexxVariable *variable = context->getLocalVariable(variableName, index);
102  RexxObject *value = variable->getVariableValue();/* get the value */
103  if (value == OREF_NULL) /* no value yet? */
104  {
105  // try the various novalue mechanisms
106  value = context->handleNovalueEvent(variableName, variableName, variable);
107  }
108  stack->push(value); /* place on the evaluation stack */
109  /* trace if necessary */
110  context->traceVariable(variableName, value);
111  return value; /* return the located variable */
112 }
113 
115  RexxVariableDictionary *dictionary)/* current activation dictionary */
116 /******************************************************************************/
117 /* Function: retrieve a simple variable's value (notready condition will */
118 /* not be raised) */
119 /******************************************************************************/
120 {
121  /* look up the name */
122  RexxVariable *variable = dictionary->getVariable(variableName);
123  RexxObject *value = variable->getVariableValue();/* get the value */
124  if (value == OREF_NULL) /* no value yet? */
125  {
126  value = this->variableName; /* just use the name */
127  }
128  return value; /* return the located variable */
129 }
130 
132  RexxActivation *context) /* current activation context */
133 /******************************************************************************/
134 /* Function: retrieve a simple variable's value (notready condition will */
135 /* not be raised) */
136 /******************************************************************************/
137 {
138  /* look up the name */
139  RexxVariable *variable = context->getLocalVariable(variableName, index);
140  RexxObject *value = variable->getVariableValue();/* get the value */
141  if (value == OREF_NULL) /* no value yet? */
142  {
143  value = this->variableName; /* just use the name */
144  }
145  return value; /* return the located variable */
146 }
147 
148 /**
149  * Retrieve an object variable value, returning OREF_NULL if
150  * the variable does not have a value.
151  *
152  * @param dictionary The source variable dictionary.
153  *
154  * @return The variable value, or OREF_NULL if the variable is not
155  * assigned.
156  */
158 {
159  /* look up the name */
160  RexxVariable *variable = dictionary->getVariable(variableName);
161  return variable->getVariableValue();/* get the value */
162 }
163 
164 
165 /**
166  * Get the value of a variable without applying a default value
167  * to it. Used in the apis so the caller can more easily
168  * detect an uninitialized variable.
169  *
170  * @param context The current context.
171  *
172  * @return The value of the variable. Returns OREF_NULL if the variable
173  * has not been assigned a value.
174  */
176 {
177  RexxVariable *variable = context->getLocalVariable(variableName, index);
178  return variable->getVariableValue();/* get the value */
179 }
180 
182  RexxVariableDictionary *dictionary, /* current activation dictionary */
183  RexxObject *value )
184 /******************************************************************************/
185 /* Function: Fast set of a variable value */
186 /******************************************************************************/
187 {
188  /* look up the name */
189  RexxVariable *variable = dictionary->getVariable(variableName);
190  variable->set(value); /* and perform the set */
191 }
192 
194  RexxActivation *context, /* current activation context */
195  RexxObject *value )
196 /******************************************************************************/
197 /* Function: Fast set of a variable value */
198 /******************************************************************************/
199 {
200  /* The context handles the details of this */
201  context->setLocalVariable(variableName, index, value);
202 }
203 
205  RexxActivation *context) /* current activation context */
206 /******************************************************************************/
207 /* Function: Check the existance of a REXX variable */
208 /******************************************************************************/
209 {
210  return context->localVariableExists(variableName, index);
211 }
212 
214  RexxActivation *context, /* current activation context */
215  RexxExpressionStack *stack, /* current evaluation stack */
216  RexxObject *value ) /* new value to assign */
217 /******************************************************************************/
218 /* Function: Assign a value to a simple variable */
219 /******************************************************************************/
220 {
221  /* The context handles the details of this */
222  context->setLocalVariable(variableName, index, value);
223  context->traceAssignment(variableName, value);
224 }
225 
227  RexxActivation *context) /* target variable dictionary */
228 /******************************************************************************/
229 /* Function: Drop a variable object */
230 /******************************************************************************/
231 {
232  /* drop the variable value */
234  // jlf: I want a trace
235  context->traceAssignment(variableName, OREF_TRACE_is_dropped, false);
236 }
237 
238 /**
239  * Drop a variable that's directly in a variable dictionary.
240  *
241  * @param dictionary The target dictionary
242  */
244 {
245  /* look up the name */
246  RexxVariable *variable = dictionary->getVariable(variableName);
247  variable->drop(); /* and perform the set */
248 }
249 
251  RexxActivation *context ) /* current activation context */
252 /******************************************************************************/
253 /* Set a guard variable notification on an object variable */
254 /******************************************************************************/
255 {
256  /* look up the name */
257  RexxVariable *variable = context->getLocalVariable(variableName, index);
258  variable->inform(ActivityManager::currentActivity); /* mark the variable entry */
259 }
260 
262  RexxActivation *context ) /* current activation context */
263 /******************************************************************************/
264 /* Remove a guard variable notification on an object variable */
265 /******************************************************************************/
266 {
267  /* look up the name */
268  RexxVariable *variable = context->getLocalVariable(variableName, index);
269  variable->uninform(ActivityManager::currentActivity); /* remove the notification */
270 }
271 
273  RexxActivation *context, /* current activation context */
274  RexxActivation *parent, /* the parent activation context */
275  RexxExpressionStack *stack) /* current evaluation stack */
276 /******************************************************************************/
277 /* Function: Expose a variable */
278 /******************************************************************************/
279 {
280  /* get the old variable entry */
281  RexxVariable *old_variable = parent->getLocalVariable(variableName, index);
282  /* set the entry in the new table */
283  context->putLocalVariable(old_variable, index);
284 }
285 
286 
288  RexxActivation *context, /* current activation context */
289  RexxExpressionStack *stack, /* current evaluation stack */
290  /* variable scope we're exposing from*/
291  RexxVariableDictionary *object_dictionary)
292 /******************************************************************************/
293 /* Function: Expose a variable */
294 /******************************************************************************/
295 {
296  /* get the old variable entry */
297  RexxVariable *old_variable = object_dictionary->getVariable(variableName);
298  /* set the entry in the new table */
299  context->putLocalVariable(old_variable, index);
300 }
301 
302 /**
303  * Return the name of this variable.
304  *
305  * @return The string value of the variable name.
306  */
308 {
309  return variableName;
310 }
311 
312 
313 void *RexxParseVariable::operator new(size_t size)
314 /******************************************************************************/
315 /* Function: Create a REXX variable translator object */
316 /******************************************************************************/
317 {
318  return new_object(size, T_VariableTerm); /* Get new object */
319 }
320 
322  RexxActivation *context) /* current activation context */
323 /******************************************************************************/
324 /* Function: Translate to upper case the contents of a variable object */
325 /******************************************************************************/
326 {
327  RexxObject *value = this->getValue(context);
328  RexxString *string = REQUEST_STRING(value);
329  ProtectedObject p(string);
330  this->set(context, string->upper());
331 }
@ T_VariableTerm
#define OREF_NULL
Definition: RexxCore.h:60
RexxString * REQUEST_STRING(RexxObject *object)
Definition: RexxCore.h:283
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
#define memory_mark(oref)
Definition: RexxMemory.hpp:445
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:493
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:446
#define cleanUpFlatten
Definition: RexxMemory.hpp:479
#define setUpFlatten(type)
Definition: RexxMemory.hpp:473
static RexxActivity *volatile currentActivity
RexxVariable * getLocalVariable(RexxString *name, size_t index)
void putLocalVariable(RexxVariable *variable, size_t index)
RexxObject * handleNovalueEvent(RexxString *name, RexxObject *defaultValue, RexxVariable *variable)
void traceAssignment(RexxString *n, RexxObject *v, bool quoteValue=true)
void setLocalVariable(RexxString *name, size_t index, RexxObject *value)
bool localVariableExists(RexxString *name, size_t index)
void traceVariable(RexxString *n, RexxObject *v)
void dropLocalVariable(RexxString *name, size_t index)
void push(RexxObject *value)
void set(RexxActivation *, RexxObject *)
RexxParseVariable(RESTORETYPE restoreType)
RexxObject * evaluate(RexxActivation *, RexxExpressionStack *)
void clearGuard(RexxActivation *)
void expose(RexxActivation *, RexxExpressionStack *, RexxVariableDictionary *)
void assign(RexxActivation *, RexxExpressionStack *, RexxObject *)
void drop(RexxActivation *)
void upper(RexxActivation *)
bool exists(RexxActivation *)
void liveGeneral(int reason)
void procedureExpose(RexxActivation *, RexxActivation *, RexxExpressionStack *)
void flatten(RexxEnvelope *)
RexxObject * getRealValue(RexxVariableDictionary *)
RexxObject * getValue(RexxVariableDictionary *)
void setGuard(RexxActivation *)
RexxString * upper()
RexxVariable * getVariable(RexxString *name)
void uninform(RexxActivity *)
void set(RexxObject *value)
RexxObject * getVariableValue()
void inform(RexxActivity *)