From 59e1402abee434c7527744189413892fab336341 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 13 Sep 2010 10:42:46 +0200 Subject: Change pragma syntax Change odb pragma namespace to db. Use qualifiers (object, value, and member) to specify type/member name. Add support for mapping C++ types to db types. --- odb/pragma.cxx | 441 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 236 insertions(+), 205 deletions(-) (limited to 'odb/pragma.cxx') diff --git a/odb/pragma.cxx b/odb/pragma.cxx index 04291e1..b11b61d 100644 --- a/odb/pragma.cxx +++ b/odb/pragma.cxx @@ -32,7 +32,7 @@ parse_scoped_name (tree& t, { if (tt != CPP_NAME) { - error ("invalid name in odb pragma %qs", prag.c_str ()); + error ("invalid name in db pragma %qs", prag.c_str ()); return 0; } @@ -46,7 +46,9 @@ parse_scoped_name (tree& t, if (scope == error_mark_node) { - error ("unable to resolve name %qs in odb pragma %qs", + error ((is_type + ? "unable to resolve type name %qs in db pragma %qs" + : "unable to resolve name %qs in db pragma %qs"), name.c_str (), prag.c_str ()); return 0; } @@ -65,7 +67,9 @@ parse_scoped_name (tree& t, if (decl == error_mark_node) { - error ("unable to resolve name %qs in odb pragma %qs", + error ((is_type + ? "unable to resolve type name %qs in db pragma %qs" + : "unable to resolve name %qs in db pragma %qs"), name.c_str (), prag.c_str ()); return 0; } @@ -84,15 +88,15 @@ check_decl_type (tree d, string const& name, string const& p, location_t l) int tc (TREE_CODE (d)); char const* pc (p.c_str ()); - if (p == "id" || + if (p == "member" || + p == "id" || p == "auto" || p == "column" || - p == "type" || p == "transient") { if (tc != FIELD_DECL) { - error_at (l, "name %qs in odb pragma %qs does not refer to " + error_at (l, "name %qs in db pragma %qs does not refer to " "a data member", name.c_str (), pc); return false; } @@ -102,14 +106,34 @@ check_decl_type (tree d, string const& name, string const& p, location_t l) { if (tc != RECORD_TYPE) { - error_at (l, "name %qs in odb pragma %qs does not refer to a class", + error_at (l, "name %qs in db pragma %qs does not refer to a class", name.c_str (), pc); return false; } } + else if (p == "value") + { + if (!TYPE_P (d)) + { + error_at (l, "name %qs in db pragma %qs does not refer to a type", + name.c_str (), pc); + return false; + } + } + else if (p == "type") + { + // Type can be used for both members and types. + // + if (tc != FIELD_DECL && !TYPE_P (d)) + { + error_at (l, "name %qs in db pragma %qs does not refer to " + "a type or data member", name.c_str (), pc); + return false; + } + } else { - error ("unknown odb pragma %qs", pc); + error ("unknown db pragma %qs", pc); return false; } @@ -117,93 +141,128 @@ check_decl_type (tree d, string const& name, string const& p, location_t l) } static void -handle_pragma (string const& p) +handle_pragma (string const& p, tree decl, string const& decl_name) { tree t; cpp_ttype tt; char const* pc (p.c_str ()); string val; - tree decl (0); location_t loc (input_location); - if (p == "object") + if (p == "table") { - // object [()] + // table ("") // + // Make sure we've got the correct declaration type. + // + if (decl != 0 && !check_decl_type (decl, decl_name, p, loc)) + return; + + if (pragma_lex (&t) != CPP_OPEN_PAREN) + { + error ("%qs expected after db pragma %qs", "(", pc); + return; + } + tt = pragma_lex (&t); - if (tt == CPP_OPEN_PAREN) + if (tt != CPP_STRING) { - tt = pragma_lex (&t); + error ("table name expected in db pragma %qs", pc); + return; + } - if (tt == CPP_NAME || tt == CPP_SCOPE) - { - string name; - decl = parse_scoped_name (t, tt, name, true, p); + val = TREE_STRING_POINTER (t); - if (decl == 0) - return; + if (pragma_lex (&t) != CPP_CLOSE_PAREN) + { + error ("%qs expected at the end of db pragma %qs", ")", pc); + return; + } - // Make sure we've got the correct declaration type. - // - if (!check_decl_type (decl, name, p, loc)) - return; + tt = pragma_lex (&t); + } + else if (p == "id") + { + // id + // - if (tt != CPP_CLOSE_PAREN) - { - error ("%qs expected at the end of odb pragma %qs", ")", pc); - return; - } + // Make sure we've got the correct declaration type. + // + if (decl != 0 && !check_decl_type (decl, decl_name, p, loc)) + return; - tt = pragma_lex (&t); - } - else - { - error ("type name expected in odb pragma %qs", pc); - return; - } - } + tt = pragma_lex (&t); } - else if (p == "table") + else if (p == "auto") { - // table ([,] "") + // auto // + // Make sure we've got the correct declaration type. + // + if (decl != 0 && !check_decl_type (decl, decl_name, p, loc)) + return; + + tt = pragma_lex (&t); + } + else if (p == "column") + { + // column ([,] "") + // + + // Make sure we've got the correct declaration type. + // + if (decl != 0 && !check_decl_type (decl, decl_name, p, loc)) + return; + if (pragma_lex (&t) != CPP_OPEN_PAREN) { - error ("%qs expected after odb pragma %qs", "(", pc); + error ("%qs expected after db pragma %qs", "(", pc); return; } tt = pragma_lex (&t); - if (tt == CPP_NAME || tt == CPP_SCOPE) + if (tt != CPP_STRING) { - string name; - decl = parse_scoped_name (t, tt, name, true, p); + error ("column name expected in db pragma %qs", pc); + return; + } - if (decl == 0) - return; + val = TREE_STRING_POINTER (t); - // Make sure we've got the correct declaration type. - // - if (!check_decl_type (decl, name, p, loc)) - return; + if (pragma_lex (&t) != CPP_CLOSE_PAREN) + { + error ("%qs expected at the end of db pragma %qs", ")", pc); + return; + } - if (tt != CPP_COMMA) - { - error ("table name expected in odb pragma %qs", pc); - return; - } + tt = pragma_lex (&t); + } + else if (p == "type") + { + // type ("") + // - tt = pragma_lex (&t); + // Make sure we've got the correct declaration type. + // + if (decl != 0 && !check_decl_type (decl, decl_name, p, loc)) + return; + + if (pragma_lex (&t) != CPP_OPEN_PAREN) + { + error ("%qs expected after db pragma %qs", "(", pc); + return; } + tt = pragma_lex (&t); + if (tt != CPP_STRING) { - error ("table name expected in odb pragma %qs", pc); + error ("type name expected in db pragma %qs", pc); return; } @@ -211,15 +270,72 @@ handle_pragma (string const& p) if (pragma_lex (&t) != CPP_CLOSE_PAREN) { - error ("%qs expected at the end of odb pragma %qs", ")", pc); + error ("%qs expected at the end of db pragma %qs", ")", pc); return; } tt = pragma_lex (&t); } - else if (p == "id") + else if (p == "transient") { - // id [()] + // transient + // + + // Make sure we've got the correct declaration type. + // + if (decl != 0 && !check_decl_type (decl, decl_name, p, loc)) + return; + + tt = pragma_lex (&t); + } + else + { + error ("unknown db pragma %qs", pc); + return; + } + + // Record this pragma. + // + pragma prag (p, val, loc); + + if (decl) + decl_pragmas_[decl].insert (prag); + else + { + tree scope (current_scope ()); + + if (!CLASS_TYPE_P (scope)) + scope = global_namespace; + + loc_pragmas_[scope].push_back (prag); + } + + // See if there are any more pragmas. + // + if (tt == CPP_NAME) + { + handle_pragma (IDENTIFIER_POINTER (t), decl, decl_name); + } + else if (tt != CPP_EOF) + error ("unexpected text after %qs in db pragma", p.c_str ()); +} + +static void +handle_pragma_qualifier (string const& p) +{ + tree t; + cpp_ttype tt; + char const* pc (p.c_str ()); + + tree decl (0); + string decl_name; + location_t loc (input_location); + + // Pragma qualifiers. + // + if (p == "object") + { + // object [()] // tt = pragma_lex (&t); @@ -230,20 +346,19 @@ handle_pragma (string const& p) if (tt == CPP_NAME || tt == CPP_SCOPE) { - string name; - decl = parse_scoped_name (t, tt, name, false, p); + decl = parse_scoped_name (t, tt, decl_name, true, p); if (decl == 0) return; // Make sure we've got the correct declaration type. // - if (!check_decl_type (decl, name, p, loc)) + if (!check_decl_type (decl, decl_name, p, loc)) return; if (tt != CPP_CLOSE_PAREN) { - error ("%qs expected at the end of odb pragma %qs", ")", pc); + error ("%qs expected at the end of db pragma %qs", ")", pc); return; } @@ -251,14 +366,14 @@ handle_pragma (string const& p) } else { - error ("data member name expected in odb pragma %qs", pc); + error ("type name expected in db pragma %qs", pc); return; } } } - else if (p == "auto") + else if (p == "value") { - // auto [()] + // value [()] // tt = pragma_lex (&t); @@ -269,20 +384,19 @@ handle_pragma (string const& p) if (tt == CPP_NAME || tt == CPP_SCOPE) { - string name; - decl = parse_scoped_name (t, tt, name, false, p); + decl = parse_scoped_name (t, tt, decl_name, true, p); if (decl == 0) return; // Make sure we've got the correct declaration type. // - if (!check_decl_type (decl, name, p, loc)) + if (!check_decl_type (decl, decl_name, p, loc)) return; if (tt != CPP_CLOSE_PAREN) { - error ("%qs expected at the end of odb pragma %qs", ")", pc); + error ("%qs expected at the end of db pragma %qs", ")", pc); return; } @@ -290,116 +404,14 @@ handle_pragma (string const& p) } else { - error ("data member name expected in odb pragma %qs", pc); - return; - } - } - } - else if (p == "column") - { - // column ([,] "") - // - - if (pragma_lex (&t) != CPP_OPEN_PAREN) - { - error ("%qs expected after odb pragma %qs", "(", pc); - return; - } - - tt = pragma_lex (&t); - - if (tt == CPP_NAME || tt == CPP_SCOPE) - { - string name; - decl = parse_scoped_name (t, tt, name, false, p); - - if (decl == 0) - return; - - // Make sure we've got the correct declaration type. - // - if (!check_decl_type (decl, name, p, loc)) - return; - - if (tt != CPP_COMMA) - { - error ("column name expected in odb pragma %qs", pc); - return; - } - - tt = pragma_lex (&t); - } - - if (tt != CPP_STRING) - { - error ("column name expected in odb pragma %qs", pc); - return; - } - - val = TREE_STRING_POINTER (t); - - if (pragma_lex (&t) != CPP_CLOSE_PAREN) - { - error ("%qs expected at the end of odb pragma %qs", ")", pc); - return; - } - - tt = pragma_lex (&t); - } - else if (p == "type") - { - // type ([,] "") - // - - if (pragma_lex (&t) != CPP_OPEN_PAREN) - { - error ("%qs expected after odb pragma %qs", "(", pc); - return; - } - - tt = pragma_lex (&t); - - if (tt == CPP_NAME || tt == CPP_SCOPE) - { - string name; - decl = parse_scoped_name (t, tt, name, false, p); - - if (decl == 0) - return; - - // Make sure we've got the correct declaration type. - // - if (!check_decl_type (decl, name, p, loc)) - return; - - if (tt != CPP_COMMA) - { - error ("type name expected in odb pragma %qs", pc); + error ("type name expected in db pragma %qs", pc); return; } - - tt = pragma_lex (&t); - } - - if (tt != CPP_STRING) - { - error ("type name expected in odb pragma %qs", pc); - return; - } - - val = TREE_STRING_POINTER (t); - - if (pragma_lex (&t) != CPP_CLOSE_PAREN) - { - error ("%qs expected at the end of odb pragma %qs", ")", pc); - return; } - - tt = pragma_lex (&t); } - else if (p == "transient") + else if (p == "member") { - // transient [()] + // member [()] // tt = pragma_lex (&t); @@ -410,20 +422,19 @@ handle_pragma (string const& p) if (tt == CPP_NAME || tt == CPP_SCOPE) { - string name; - decl = parse_scoped_name (t, tt, name, false, p); + decl = parse_scoped_name (t, tt, decl_name, false, p); if (decl == 0) return; // Make sure we've got the correct declaration type. // - if (!check_decl_type (decl, name, p, loc)) + if (!check_decl_type (decl, decl_name, p, loc)) return; if (tt != CPP_CLOSE_PAREN) { - error ("%qs expected at the end of odb pragma %qs", ")", pc); + error ("%qs expected at the end of db pragma %qs", ")", pc); return; } @@ -431,20 +442,33 @@ handle_pragma (string const& p) } else { - error ("data member name expected in odb pragma %qs", pc); + error ("data member name expected in db pragma %qs", pc); return; } } } + // + // The member qualifier can be omitted so we need to also handle all + // the member pragmas here. + // + else if (p == "id" || + p == "auto" || + p == "column" || + p == "type" || + p == "transient") + { + handle_pragma (p, 0, ""); + return; + } else { - error ("unknown odb pragma %qs", pc); + error ("unknown db pragma %qs", pc); return; } // Record this pragma. // - pragma prag (p, val, loc); + pragma prag (p, "", loc); if (decl) decl_pragmas_[decl].insert (prag); @@ -462,62 +486,69 @@ handle_pragma (string const& p) // if (tt == CPP_NAME) { - handle_pragma (IDENTIFIER_POINTER (t)); + handle_pragma (IDENTIFIER_POINTER (t), decl, decl_name); } else if (tt != CPP_EOF) - error ("unexpected text after %qs in odb pragma", p.c_str ()); + error ("unexpected text after %qs in db pragma", p.c_str ()); +} + +extern "C" void +handle_pragma_db_object (cpp_reader*) +{ + handle_pragma_qualifier ("object"); } extern "C" void -handle_pragma_odb_object (cpp_reader*) +handle_pragma_db_value (cpp_reader*) { - handle_pragma ("object"); + handle_pragma_qualifier ("value"); } extern "C" void -handle_pragma_odb_table (cpp_reader*) +handle_pragma_db_member (cpp_reader*) { - handle_pragma ("table"); + handle_pragma_qualifier ("member"); } extern "C" void -handle_pragma_odb_id (cpp_reader*) +handle_pragma_db_id (cpp_reader*) { - handle_pragma ("id"); + handle_pragma_qualifier ("id"); } extern "C" void -handle_pragma_odb_auto (cpp_reader*) +handle_pragma_db_auto (cpp_reader*) { - handle_pragma ("auto"); + handle_pragma_qualifier ("auto"); } extern "C" void -handle_pragma_odb_column (cpp_reader*) +handle_pragma_db_column (cpp_reader*) { - handle_pragma ("column"); + handle_pragma_qualifier ("column"); } extern "C" void -handle_pragma_odb_type (cpp_reader*) +handle_pragma_db_type (cpp_reader*) { - handle_pragma ("type"); + handle_pragma_qualifier ("type"); } extern "C" void -handle_pragma_odb_transient (cpp_reader*) +handle_pragma_db_transient (cpp_reader*) { - handle_pragma ("transient"); + handle_pragma_qualifier ("transient"); } extern "C" void register_odb_pragmas (void*, void*) { - c_register_pragma_with_expansion ("odb", "object", handle_pragma_odb_object); - c_register_pragma_with_expansion ("odb", "table", handle_pragma_odb_table); - c_register_pragma_with_expansion ("odb", "id", handle_pragma_odb_id); - c_register_pragma_with_expansion ("odb", "auto", handle_pragma_odb_auto); - c_register_pragma_with_expansion ("odb", "column", handle_pragma_odb_column); - c_register_pragma_with_expansion ("odb", "type", handle_pragma_odb_type); - c_register_pragma_with_expansion ("odb", "transient", handle_pragma_odb_transient); + c_register_pragma_with_expansion ("db", "object", handle_pragma_db_object); + c_register_pragma_with_expansion ("db", "value", handle_pragma_db_value); + c_register_pragma_with_expansion ("db", "member", handle_pragma_db_member); + c_register_pragma_with_expansion ("db", "id", handle_pragma_db_id); + c_register_pragma_with_expansion ("db", "auto", handle_pragma_db_auto); + c_register_pragma_with_expansion ("db", "column", handle_pragma_db_column); + c_register_pragma_with_expansion ("db", "type", handle_pragma_db_type); + c_register_pragma_with_expansion ("db", "transient", handle_pragma_db_transient); } -- cgit v1.1