2
/* Copyright (C) 2002 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; version 2 of the License.
8
This program 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 this program; if not, write to the Free Software
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
#ifndef _SP_PCONTEXT_H_
18
#define _SP_PCONTEXT_H_
20
#ifdef USE_PRAGMA_INTERFACE
21
#pragma interface /* gcc class implementation */
31
typedef struct sp_variable
34
enum enum_field_types type;
38
offset -- this the index to the variable's value in the runtime frame.
39
This is calculated during parsing and used when creating sp_instr_set
40
instructions and Item_splocal items.
41
I.e. values are set/referred by array indexing in runtime.
46
Create_field field_def;
50
#define SP_LAB_IMPL 0 // Implicit label generated by parser
51
#define SP_LAB_BEGIN 1 // Label at BEGIN
52
#define SP_LAB_ITER 2 // Label at iteration control
55
An SQL/PSM label. Can refer to the identifier used with the
56
"label_name:" construct which may precede some SQL/PSM statements, or
57
to an implicit implementation-dependent identifier which the parser
58
inserts before a high-level flow control statement such as
59
IF/WHILE/REPEAT/LOOP, when such statement is rewritten into
60
a combination of low-level jump/jump_if instructions and labels.
63
typedef struct sp_label
66
uint ip; // Instruction index
67
int type; // begin/iter or ref/free
68
sp_pcontext *ctx; // The label's context
71
typedef struct sp_cond_type
73
enum { number, state, warning, notfound, exception } type;
74
char sqlstate[SQLSTATE_LENGTH+1];
79
Sanity check for SQLSTATEs. Will not check if it's really an existing
80
state (there are just too many), but will check length bad characters.
83
sp_cond_check(LEX_STRING *sqlstate);
85
typedef struct sp_cond
92
The scope of a label in Stored Procedures,
93
for name resolution of labels in a parsing context.
98
The labels declared in a parent context are in scope.
102
The labels declared in a parent context are not in scope.
108
The parse-time context, used to keep track of declared variables/parameters,
109
conditions, handlers, cursors and labels, during parsing.
110
sp_contexts are organized as a tree, with one object for each begin-end
111
block, one object for each exception handler,
112
plus a root-context for the parameters.
113
This is used during parsing for looking up defined names (e.g. declared
114
variables and visible labels), for error checking, and to calculate offsets
115
to be used at runtime. (During execution variable values, active handlers
116
and cursors, etc, are referred to by an index in a stack.)
117
Parsing contexts for exception handlers limit the visibility of labels.
118
The pcontext tree is also kept during execution and is used for error
119
checking (e.g. correct number of parameters), and in the future, used by
123
class sp_pcontext : public Sql_alloc
129
Builds a parsing context root node.
138
Create and push a new context in the tree.
139
@param label_scope label scope for the new parsing context
140
@return the node created
143
push_context(label_scope_type label_scope);
146
Pop a node from the parsing context tree.
147
@return the parent node
159
Number of handlers/cursors to pop between this context and 'ctx'.
160
If 'exclusive' is true, don't count the last block we are leaving;
161
this is used for LEAVE where we will jump to the cpop/hpop instructions.
164
diff_handlers(sp_pcontext *ctx, bool exclusive);
166
diff_cursors(sp_pcontext *ctx, bool exclusive);
170
// Parameters and variables
174
The maximum number of variables used in this and all child contexts
175
In the root, this gives us the number of slots needed for variables
181
return m_max_var_index;
185
The current number of variables used in the parents (from the root),
186
including this context.
191
return m_var_offset + m_vars.elements;
194
/* The number of variables in this context alone */
198
return m_vars.elements;
201
/* Map index in this pcontext to runtime offset */
203
var_context2runtime(uint i)
205
return m_var_offset + i;
208
/* Set type of variable. 'i' is the offset from the top */
210
set_type(uint i, enum enum_field_types type)
212
sp_variable_t *p= find_variable(i);
218
/* Set default value of variable. 'i' is the offset from the top */
220
set_default(uint i, Item *it)
222
sp_variable_t *p= find_variable(i);
229
push_variable(LEX_STRING *name, enum enum_field_types type,
230
sp_param_mode_t mode);
233
Retrieve definitions of fields from the current context and its
237
retrieve_field_definitions(List<Create_field> *field_def_lst);
241
find_variable(LEX_STRING *name, my_bool scoped=0);
243
// Find by offset (from the top)
245
find_variable(uint offset);
248
Set the current scope boundary (for default values).
249
The argument is the number of variables to skip.
252
declare_var_boundary(uint n)
258
CASE expressions support.
264
return m_num_case_exprs++;
268
get_num_case_exprs() const
270
return m_num_case_exprs;
274
push_case_expr_id(int case_expr_id)
276
return insert_dynamic(&m_case_expr_id_lst, (uchar*) &case_expr_id);
282
pop_dynamic(&m_case_expr_id_lst);
286
get_current_case_expr_id() const
290
get_dynamic((DYNAMIC_ARRAY*)&m_case_expr_id_lst, (uchar*) &case_expr_id,
291
m_case_expr_id_lst.elements - 1);
301
push_label(char *name, uint ip);
304
find_label(char *name);
309
sp_label_t *lab= m_label.head();
311
if (!lab && m_parent)
312
lab= m_parent->last_label();
319
return m_label.pop();
327
push_cond(LEX_STRING *name, sp_cond_type_t *val);
333
pop_dynamic(&m_conds);
337
find_cond(LEX_STRING *name, my_bool scoped=0);
344
push_handler(sp_cond_type_t *cond)
346
insert_dynamic(&m_handlers, (uchar*)&cond);
350
find_handler(sp_cond_type *cond);
355
return m_max_handler_index + m_context_handlers;
361
m_context_handlers+= n;
369
push_cursor(LEX_STRING *name);
372
find_cursor(LEX_STRING *name, uint *poff, my_bool scoped=0);
374
/* Find by offset (for debugging only) */
376
find_cursor(uint offset, LEX_STRING *n);
381
return m_max_cursor_index + m_cursors.elements;
385
current_cursor_count()
387
return m_cursor_offset + m_cursors.elements;
393
Constructor for a tree node.
394
@param prev the parent parsing context
395
@param label_scope label_scope for this parsing context
397
sp_pcontext(sp_pcontext *prev, label_scope_type label_scope);
400
m_max_var_index -- number of variables (including all types of arguments)
401
in this context including all children contexts.
403
m_max_var_index >= m_vars.elements.
405
m_max_var_index of the root parsing context contains number of all
406
variables (including arguments) in all enclosed contexts.
408
uint m_max_var_index;
410
// The maximum sub context's framesizes
411
uint m_max_cursor_index;
412
uint m_max_handler_index;
413
uint m_context_handlers; // No. of handlers in this context
417
sp_pcontext *m_parent; // Parent context
420
m_var_offset -- this is an index of the first variable in this
423
m_var_offset is 0 for root context.
425
Since now each variable is stored in separate place, no reuse is done,
426
so m_var_offset is different for all enclosed contexts.
430
uint m_cursor_offset; // Cursor offset for this context
433
Boundary for finding variables in this context. This is the number
434
of variables currently "invisible" to default clauses.
435
This is normally 0, but will be larger during parsing of
436
DECLARE ... DEFAULT, to get the scope right for DEFAULT values.
440
int m_num_case_exprs;
442
DYNAMIC_ARRAY m_vars; // Parameters/variables
443
DYNAMIC_ARRAY m_case_expr_id_lst; /* Stack of CASE expression ids. */
444
DYNAMIC_ARRAY m_conds; // Conditions
445
DYNAMIC_ARRAY m_cursors; // Cursors
446
DYNAMIC_ARRAY m_handlers; // Handlers, for checking for duplicates
448
List<sp_label_t> m_label; // The label list
450
List<sp_pcontext> m_children; // Children contexts, used for destruction
453
Scope of labels for this parsing context.
455
label_scope_type m_label_scope;
458
sp_pcontext(const sp_pcontext &); /* Prevent use of these */
459
void operator=(sp_pcontext &);
460
}; // class sp_pcontext : public Sql_alloc
463
#endif /* _SP_PCONTEXT_H_ */