diff options
Diffstat (limited to 'sql')
-rw-r--r-- | sql/sp_head.cc | 2 | ||||
-rw-r--r-- | sql/sp_head.h | 2 | ||||
-rw-r--r-- | sql/sp_pcontext.cc | 90 | ||||
-rw-r--r-- | sql/sp_pcontext.h | 61 | ||||
-rw-r--r-- | sql/sql_yacc.yy | 43 |
5 files changed, 136 insertions, 62 deletions
diff --git a/sql/sp_head.cc b/sql/sp_head.cc index c1643f0f82e..63ee37e1135 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -470,7 +470,7 @@ sp_head::init(LEX *lex) { DBUG_ENTER("sp_head::init"); - lex->spcont= m_pcont= new sp_pcontext(NULL); + lex->spcont= m_pcont= new sp_pcontext(); /* Altough trg_table_fields list is used only in triggers we init for all diff --git a/sql/sp_head.h b/sql/sp_head.h index 4ef4077cc79..901b7a19c39 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -107,8 +107,6 @@ public: /* Possible values of m_flags */ enum { HAS_RETURN= 1, // For FUNCTIONs only: is set if has RETURN - IN_SIMPLE_CASE= 2, // Is set if parsing a simple CASE - IN_HANDLER= 4, // Is set if the parser is in a handler body MULTI_RESULTS= 8, // Is set if a procedure with SELECT(s) CONTAINS_DYNAMIC_SQL= 16, // Is set if a procedure with PREPARE/EXECUTE IS_INVOKED= 32, // Is set if this sp_head is being used diff --git a/sql/sp_pcontext.cc b/sql/sp_pcontext.cc index 6229cf14604..780243cc79f 100644 --- a/sql/sp_pcontext.cc +++ b/sql/sp_pcontext.cc @@ -25,6 +25,11 @@ #include "sp_pcontext.h" #include "sp_head.h" +/* Initial size for the dynamic arrays in sp_pcontext */ +#define PCONTEXT_ARRAY_INIT_ALLOC 16 +/* Increment size for the dynamic arrays in sp_pcontext */ +#define PCONTEXT_ARRAY_INCREMENT_ALLOC 8 + /* Sanity check for SQLSTATEs. Will not check if it's really an existing state (there are just too many), but will check length and bad characters. @@ -49,28 +54,61 @@ sp_cond_check(LEX_STRING *sqlstate) return TRUE; } -sp_pcontext::sp_pcontext(sp_pcontext *prev) - :Sql_alloc(), m_max_var_index(0), m_max_cursor_index(0), m_max_handler_index(0), - m_context_handlers(0), m_parent(prev), m_pboundary(0) +sp_pcontext::sp_pcontext() + : Sql_alloc(), + m_max_var_index(0), m_max_cursor_index(0), m_max_handler_index(0), + m_context_handlers(0), m_parent(NULL), m_pboundary(0), + m_label_scope(LABEL_DEFAULT_SCOPE) { - VOID(my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), 16, 8)); - VOID(my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), 16, 8)); - VOID(my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), 16, 8)); - VOID(my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), 16, 8)); - VOID(my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), 16, 8)); + VOID(my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); m_label.empty(); m_children.empty(); - if (!prev) - { - m_var_offset= m_cursor_offset= 0; - m_num_case_exprs= 0; - } - else - { - m_var_offset= prev->m_var_offset + prev->m_max_var_index; - m_cursor_offset= prev->current_cursor_count(); - m_num_case_exprs= prev->get_num_case_exprs(); - } + + m_var_offset= m_cursor_offset= 0; + m_num_case_exprs= 0; +} + +sp_pcontext::sp_pcontext(sp_pcontext *prev, label_scope_type label_scope) + : Sql_alloc(), + m_max_var_index(0), m_max_cursor_index(0), m_max_handler_index(0), + m_context_handlers(0), m_parent(prev), m_pboundary(0), + m_label_scope(label_scope) +{ + VOID(my_init_dynamic_array(&m_vars, sizeof(sp_variable_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_case_expr_id_lst, sizeof(int), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_conds, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_cursors, sizeof(LEX_STRING), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + VOID(my_init_dynamic_array(&m_handlers, sizeof(sp_cond_type_t *), + PCONTEXT_ARRAY_INIT_ALLOC, + PCONTEXT_ARRAY_INCREMENT_ALLOC)); + m_label.empty(); + m_children.empty(); + + m_var_offset= prev->m_var_offset + prev->m_max_var_index; + m_cursor_offset= prev->current_cursor_count(); + m_num_case_exprs= prev->get_num_case_exprs(); } void @@ -92,9 +130,9 @@ sp_pcontext::destroy() } sp_pcontext * -sp_pcontext::push_context() +sp_pcontext::push_context(label_scope_type label_scope) { - sp_pcontext *child= new sp_pcontext(this); + sp_pcontext *child= new sp_pcontext(this, label_scope); if (child) m_children.push_back(child); @@ -257,7 +295,15 @@ sp_pcontext::find_label(char *name) if (my_strcasecmp(system_charset_info, name, lab->name) == 0) return lab; - if (m_parent) + /* + Note about exception handlers. + See SQL:2003 SQL/PSM (ISO/IEC 9075-4:2003), + section 13.1 <compound statement>, + syntax rule 4. + In short, a DECLARE HANDLER block can not refer + to labels from the parent context, as they are out of scope. + */ + if (m_parent && (m_label_scope == LABEL_DEFAULT_SCOPE)) return m_parent->find_label(name); return NULL; } diff --git a/sql/sp_pcontext.h b/sql/sp_pcontext.h index b2cdd5e689c..5bffda79f98 100644 --- a/sql/sp_pcontext.h +++ b/sql/sp_pcontext.h @@ -88,16 +88,33 @@ typedef struct sp_cond sp_cond_type_t *val; } sp_cond_t; +/** + The scope of a label in Stored Procedures, + for name resolution of labels in a parsing context. +*/ +enum label_scope_type +{ + /** + The labels declared in a parent context are in scope. + */ + LABEL_DEFAULT_SCOPE, + /** + The labels declared in a parent context are not in scope. + */ + LABEL_HANDLER_SCOPE +}; -/* - The parse-time context, used to keep track on declared variables/parameters, +/** + The parse-time context, used to keep track of 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. + block, one object for each exception handler, + 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.) + Parsing contexts for exception handlers limit the visibility of labels. 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. @@ -105,21 +122,30 @@ typedef struct sp_cond class sp_pcontext : public Sql_alloc { - sp_pcontext(const sp_pcontext &); /* Prevent use of these */ - void operator=(sp_pcontext &); +public: - public: - - sp_pcontext(sp_pcontext *prev); + /** + Constructor. + Builds a parsing context root node. + */ + sp_pcontext(); // Free memory void destroy(); + /** + Create and push a new context in the tree. + @param label_scope label scope for the new parsing context + @return the node created + */ sp_pcontext * - push_context(); + push_context(label_scope_type label_scope); - // Returns the previous context, not the one we pop + /** + Pop a node from the parsing context tree. + @return the parent node + */ sp_pcontext * pop_context(); @@ -363,6 +389,13 @@ class sp_pcontext : public Sql_alloc protected: + /** + Constructor for a tree node. + @param prev the parent parsing context + @param label_scope label_scope for this parsing context + */ + sp_pcontext(sp_pcontext *prev, label_scope_type label_scope); + /* m_max_var_index -- number of variables (including all types of arguments) in this context including all children contexts. @@ -416,6 +449,14 @@ private: List<sp_pcontext> m_children; // Children contexts, used for destruction + /** + Scope of labels for this parsing context. + */ + label_scope_type m_label_scope; + +private: + sp_pcontext(const sp_pcontext &); /* Prevent use of these */ + void operator=(sp_pcontext &); }; // class sp_pcontext : public Sql_alloc diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index d07234ff2bd..812482e8ffb 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -2006,6 +2006,9 @@ sp_decl: { LEX *lex= Lex; sp_head *sp= lex->sphead; + + lex->spcont= lex->spcont->push_context(LABEL_HANDLER_SCOPE); + sp_pcontext *ctx= lex->spcont; sp_instr_hpush_jump *i= new sp_instr_hpush_jump(sp->instructions(), ctx, $2, @@ -2013,7 +2016,6 @@ sp_decl: sp->add_instr(i); sp->push_backpatch(i, ctx->push_label((char *)"", 0)); - sp->m_flags|= sp_head::IN_HANDLER; } sp_hcond_list sp_proc_stmt { @@ -2037,10 +2039,12 @@ sp_decl: sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */ } lex->sphead->backpatch(hlab); - sp->m_flags&= ~sp_head::IN_HANDLER; + + lex->spcont= ctx->pop_context(); + $$.vars= $$.conds= $$.curs= 0; $$.hndlrs= $6; - ctx->add_handlers($6); + lex->spcont->add_handlers($6); } | DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt { @@ -2103,11 +2107,18 @@ sp_handler_type: ; sp_hcond_list: + sp_hcond_element + { $$= 1; } + | sp_hcond_list ',' sp_hcond_element + { $$+= 1; } + ; + +sp_hcond_element: sp_hcond { LEX *lex= Lex; sp_head *sp= lex->sphead; - sp_pcontext *ctx= lex->spcont; + sp_pcontext *ctx= lex->spcont->parent_context(); if (ctx->find_handler($1)) { @@ -2121,28 +2132,6 @@ sp_hcond_list: i->add_condition($1); ctx->push_handler($1); - $$= 1; - } - } - | sp_hcond_list ',' sp_hcond - { - LEX *lex= Lex; - sp_head *sp= lex->sphead; - sp_pcontext *ctx= lex->spcont; - - if (ctx->find_handler($3)) - { - my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0)); - MYSQL_YYABORT; - } - else - { - sp_instr_hpush_jump *i= - (sp_instr_hpush_jump *)sp->last_instruction(); - - i->add_condition($3); - ctx->push_handler($3); - $$= $1 + 1; } } ; @@ -2687,7 +2676,7 @@ sp_unlabeled_control: sp_label_t *lab= lex->spcont->last_label(); lab->type= SP_LAB_BEGIN; - lex->spcont= lex->spcont->push_context(); + lex->spcont= lex->spcont->push_context(LABEL_DEFAULT_SCOPE); } sp_decls sp_proc_stmts |