summaryrefslogtreecommitdiff
path: root/sql/sp_pcontext.h
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sp_pcontext.h')
-rw-r--r--sql/sp_pcontext.h130
1 files changed, 78 insertions, 52 deletions
diff --git a/sql/sp_pcontext.h b/sql/sp_pcontext.h
index 872c7c1d505..e61057537da 100644
--- a/sql/sp_pcontext.h
+++ b/sql/sp_pcontext.h
@@ -29,22 +29,23 @@ typedef enum
sp_param_inout
} sp_param_mode_t;
-typedef struct sp_pvar
+typedef struct sp_variable
{
LEX_STRING name;
enum enum_field_types type;
sp_param_mode_t mode;
/*
- offset -- basically, this is an index of variable in the scope of root
- parsing context. This means, that all variables in a stored routine
- have distinct indexes/offsets.
+ offset -- this the index to the variable's value in the runtime frame.
+ This is calculated during parsing and used when creating sp_instr_set
+ instructions and Item_splocal items.
+ I.e. values are set/referred by array indexing in runtime.
*/
uint offset;
Item *dflt;
create_field field_def;
-} sp_pvar_t;
+} sp_variable_t;
#define SP_LAB_REF 0 // Unresolved reference (for goto)
@@ -76,9 +77,10 @@ typedef struct sp_cond_type
uint mysqlerr;
} sp_cond_type_t;
-/* Sanity check for SQLSTATEs. Will not check if it's really an existing
- * state (there are just too many), but will check length bad characters.
- */
+/*
+ Sanity check for SQLSTATEs. Will not check if it's really an existing
+ state (there are just too many), but will check length bad characters.
+*/
extern bool
sp_cond_check(LEX_STRING *sqlstate);
@@ -90,7 +92,17 @@ typedef struct sp_cond
/*
- This seems to be an "SP parsing context" or something.
+ The parse-time context, used to keep track on declared variables/parameters,
+ conditions, handlers, cursors and labels, during parsing.
+ sp_contexts are organized as a tree, with one object for each begin-end
+ block, plus a root-context for the parameters.
+ This is used during parsing for looking up defined names (e.g. declared
+ variables and visible labels), for error checking, and to calculate offsets
+ to be used at runtime. (During execution variable values, active handlers
+ and cursors, etc, are referred to by an index in a stack.)
+ The pcontext tree is also kept during execution and is used for error
+ checking (e.g. correct number of parameters), and in the future, used by
+ the debugger.
*/
class sp_pcontext : public Sql_alloc
@@ -134,50 +146,64 @@ class sp_pcontext : public Sql_alloc
// Parameters and variables
//
+ /*
+ The maximum number of variables used in this and all child contexts
+ In the root, this gives us the number of slots needed for variables
+ during execution.
+ */
inline uint
- total_pvars()
+ max_var_index()
{
- return m_total_pvars;
+ return m_max_var_index;
}
+ /*
+ The current number of variables used in the parents (from the root),
+ including this context.
+ */
inline uint
- current_pvars()
+ current_var_count()
{
- return m_poffset + m_pvar.elements;
+ return m_var_offset + m_vars.elements;
}
+ /* The number of variables in this context alone */
inline uint
- context_pvars()
+ context_var_count()
{
- return m_pvar.elements;
+ return m_vars.elements;
}
+ /* Map index in this pcontext to runtime offset */
inline uint
- pvar_context2index(uint i)
+ var_context2runtime(uint i)
{
- return m_poffset + i;
+ return m_var_offset + i;
}
+ /* Set type of variable. 'i' is the offset from the top */
inline void
set_type(uint i, enum enum_field_types type)
{
- sp_pvar_t *p= find_pvar(i);
+ sp_variable_t *p= find_variable(i);
if (p)
p->type= type;
}
+ /* Set default value of variable. 'i' is the offset from the top */
inline void
set_default(uint i, Item *it)
{
- sp_pvar_t *p= find_pvar(i);
+ sp_variable_t *p= find_variable(i);
if (p)
p->dflt= it;
}
- sp_pvar_t *
- push_pvar(LEX_STRING *name, enum enum_field_types type, sp_param_mode_t mode);
+ sp_variable_t *
+ push_variable(LEX_STRING *name, enum enum_field_types type,
+ sp_param_mode_t mode);
/*
Retrieve definitions of fields from the current context and its
@@ -187,12 +213,12 @@ class sp_pcontext : public Sql_alloc
retrieve_field_definitions(List<create_field> *field_def_lst);
// Find by name
- sp_pvar_t *
- find_pvar(LEX_STRING *name, my_bool scoped=0);
+ sp_variable_t *
+ find_variable(LEX_STRING *name, my_bool scoped=0);
- // Find by offset
- sp_pvar_t *
- find_pvar(uint offset);
+ // Find by offset (from the top)
+ sp_variable_t *
+ find_variable(uint offset);
/*
Set the current scope boundary (for default values).
@@ -280,7 +306,7 @@ class sp_pcontext : public Sql_alloc
pop_cond(uint num)
{
while (num--)
- pop_dynamic(&m_cond);
+ pop_dynamic(&m_conds);
}
sp_cond_type_t *
@@ -293,22 +319,22 @@ class sp_pcontext : public Sql_alloc
inline void
push_handler(sp_cond_type_t *cond)
{
- insert_dynamic(&m_handler, (gptr)&cond);
+ insert_dynamic(&m_handlers, (gptr)&cond);
}
bool
find_handler(sp_cond_type *cond);
inline uint
- max_handlers()
+ max_handler_index()
{
- return m_hsubsize + m_handlers;
+ return m_max_handler_index + m_context_handlers;
}
inline void
add_handlers(uint n)
{
- m_handlers+= n;
+ m_context_handlers+= n;
}
//
@@ -326,51 +352,51 @@ class sp_pcontext : public Sql_alloc
find_cursor(uint offset, LEX_STRING *n);
inline uint
- max_cursors()
+ max_cursor_index()
{
- return m_csubsize + m_cursor.elements;
+ return m_max_cursor_index + m_cursors.elements;
}
inline uint
- current_cursors()
+ current_cursor_count()
{
- return m_coffset + m_cursor.elements;
+ return m_cursor_offset + m_cursors.elements;
}
protected:
/*
- m_total_pvars -- number of variables (including all types of arguments)
+ m_max_var_index -- number of variables (including all types of arguments)
in this context including all children contexts.
- m_total_pvars >= m_pvar.elements.
+ m_max_var_index >= m_vars.elements.
- m_total_pvars of the root parsing context contains number of all
+ m_max_var_index of the root parsing context contains number of all
variables (including arguments) in all enclosed contexts.
*/
- uint m_total_pvars;
+ uint m_max_var_index;
// The maximum sub context's framesizes
- uint m_csubsize;
- uint m_hsubsize;
- uint m_handlers; // No. of handlers in this context
+ uint m_max_cursor_index;
+ uint m_max_handler_index;
+ uint m_context_handlers; // No. of handlers in this context
private:
sp_pcontext *m_parent; // Parent context
/*
- m_poffset -- basically, this is an index of the first variable in this
- parsing context.
+ m_var_offset -- this is an index of the first variable in this
+ parsing context.
- m_poffset is 0 for root context.
+ m_var_offset is 0 for root context.
Since now each variable is stored in separate place, no reuse is done,
- so m_poffset is different for all enclosed contexts.
+ so m_var_offset is different for all enclosed contexts.
*/
- uint m_poffset;
+ uint m_var_offset;
- uint m_coffset; // Cursor offset for this context
+ uint m_cursor_offset; // Cursor offset for this context
/*
Boundary for finding variables in this context. This is the number
@@ -382,11 +408,11 @@ private:
int m_num_case_exprs;
- DYNAMIC_ARRAY m_pvar; // Parameters/variables
+ DYNAMIC_ARRAY m_vars; // Parameters/variables
DYNAMIC_ARRAY m_case_expr_id_lst; /* Stack of CASE expression ids. */
- DYNAMIC_ARRAY m_cond; // Conditions
- DYNAMIC_ARRAY m_cursor; // Cursors
- DYNAMIC_ARRAY m_handler; // Handlers, for checking of duplicates
+ DYNAMIC_ARRAY m_conds; // Conditions
+ DYNAMIC_ARRAY m_cursors; // Cursors
+ DYNAMIC_ARRAY m_handlers; // Handlers, for checking for duplicates
List<sp_label_t> m_label; // The label list