From cc3979f34a886ae4c89d4e3e86a5b0db1669585f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 5 Nov 2012 11:46:03 +0200 Subject: Move some of the preprocessing from relational to common --- odb/processor.cxx | 1365 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 1240 insertions(+), 125 deletions(-) (limited to 'odb/processor.cxx') diff --git a/odb/processor.cxx b/odb/processor.cxx index 616eddc..d5f8c19 100644 --- a/odb/processor.cxx +++ b/odb/processor.cxx @@ -19,6 +19,16 @@ using namespace std; namespace { + // Indirect (dynamic) context values. + // + static semantics::type* + id_tree_type () + { + context& c (context::current ()); + semantics::data_member& id (*context::id_member (*c.top_object)); + return &id.type (); + } + struct data_member: traversal::data_member, context { virtual void @@ -29,6 +39,64 @@ namespace process_access (m, "get"); process_access (m, "set"); + + // We don't need to do any further processing for common if we + // are generating static multi-database code. + // + if (options.database ()[0] == database::common && + options.multi_database () == multi_database::static_) + return; + + semantics::names* hint; + semantics::type& t (utype (m, hint)); + + // Handle wrappers. + // + semantics::type* wt (0), *qwt (0); + semantics::names* whint (0); + if (process_wrapper (t)) + { + qwt = t.get ("wrapper-type"); + whint = t.get ("wrapper-hint"); + wt = &utype (*qwt, whint); + } + + // If the type is const and the member is not id, version, or + // inverse, then mark it as readonly. In case of a wrapper, + // both the wrapper type and the wrapped type must be const. + // To see why, consider these possibilities: + // + // auto_ptr - can modify by setting a new pointer + // const auto_ptr - can modify by changing the pointed-to value + // + if (const_type (m.type ()) && + !(id (m) || version (m) || m.count ("inverse"))) + { + if (qwt == 0 || const_type (*qwt)) + m.set ("readonly", true); + } + + if (composite_wrapper (t)) + return; + + // Process object pointer. The resulting column will be a simple + // or composite value. + // + if (process_object_pointer (m, t)) + return; + + // Before checking if this is a container, check if this member + // or its type were deduced to be a simple value based on the + // pragmas. This is necessary because a container member (e.g., + // vector) can be "overridden" into a simple value (e.g., + // BLOB) with a pragma. + // + if (m.count ("simple") || + t.count ("simple") || + (wt != 0 && wt->count ("simple"))) + return; + + process_container (m, (wt != 0 ? *wt : t)); } // @@ -518,187 +586,1116 @@ namespace } } } - }; - struct class_: traversal::class_, context - { - class_ () - : std_string_ (0), std_string_hint_ (0), access_ (0) + // + // Process wrapper. + // + + bool + process_wrapper (semantics::type& t) { - *this >> member_names_ >> member_; + if (t.count ("wrapper")) + return t.get ("wrapper"); - // Resolve the std::string type node. + // Check this type with wrapper_traits. // - using semantics::scope; + tree inst (instantiate_template (wrapper_traits_, t.tree_node ())); - for (scope::names_iterator_pair ip (unit.find ("std")); - ip.first != ip.second; ++ip.first) + if (inst == 0) { - if (scope* ns = dynamic_cast (&ip.first->named ())) - { - scope::names_iterator_pair jp (ns->find ("string")); + t.set ("wrapper", false); + return false; + } - if (jp.first != jp.second) - { - std_string_ = dynamic_cast ( - &jp.first->named ()); - std_string_hint_ = &*jp.first; + // @@ This points to the primary template, not the specialization. + // + tree decl (TYPE_NAME (inst)); + + string f (DECL_SOURCE_FILE (decl)); + size_t l (DECL_SOURCE_LINE (decl)); + size_t c (DECL_SOURCE_COLUMN (decl)); + + // Get the wrapped type. + // + try + { + tree decl ( + lookup_qualified_name ( + inst, get_identifier ("wrapped_type"), true, false)); + + if (decl == error_mark_node || TREE_CODE (decl) != TYPE_DECL) + throw operation_failed (); + + // The wrapped_type alias is a typedef in an instantiation + // that we just instantiated dynamically. As a result there + // is no semantic graph edges corresponding to this typedef + // since we haven't parsed it yet. So to get the tree node + // that can actually be resolved to the graph node, we use + // the source type of this typedef. + // + tree type (DECL_ORIGINAL_TYPE (decl)); + + semantics::type& wt ( + dynamic_cast (*unit.find (type))); + + // Find the hint. + // + semantics::names* wh (0); + + for (tree ot (DECL_ORIGINAL_TYPE (decl)); + ot != 0; + ot = decl ? DECL_ORIGINAL_TYPE (decl) : 0) + { + if ((wh = unit.find_hint (ot))) break; - } + + decl = TYPE_NAME (ot); } - } - assert (std_string_ != 0); // No std::string? + t.set ("wrapper-type", &wt); + t.set ("wrapper-hint", wh); + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "wrapper_traits specialization does not define the " + << "wrapped_type type" << endl; + throw; + } - // Resolve odb::access, if any. + // Get the null_handler flag. // - tree odb = lookup_qualified_name ( - global_namespace, get_identifier ("odb"), false, false); + bool null_handler (false); - if (odb != error_mark_node) + try { - access_ = lookup_qualified_name ( - odb, get_identifier ("access"), true, false); + tree nh ( + lookup_qualified_name ( + inst, get_identifier ("null_handler"), false, false)); - access_ = (access_ != error_mark_node ? TREE_TYPE (access_) : 0); - } - } + if (nh == error_mark_node || TREE_CODE (nh) != VAR_DECL) + throw operation_failed (); - virtual void - traverse (type& c) - { - class_kind_type k (class_kind (c)); + // Instantiate this decalaration so that we can get its value. + // + if (DECL_TEMPLATE_INSTANTIATION (nh) && + !DECL_TEMPLATE_INSTANTIATED (nh) && + !DECL_EXPLICIT_INSTANTIATION (nh)) + instantiate_decl (nh, false, false); - if (k == class_other) - return; + tree init (DECL_INITIAL (nh)); - // Check if odb::access is a friend of this class. + if (init == error_mark_node || TREE_CODE (init) != INTEGER_CST) + throw operation_failed (); + + unsigned long long e; + + { + HOST_WIDE_INT hwl (TREE_INT_CST_LOW (init)); + HOST_WIDE_INT hwh (TREE_INT_CST_HIGH (init)); + + unsigned long long l (hwl); + unsigned long long h (hwh); + unsigned short width (HOST_BITS_PER_WIDE_INT); + + e = (h << width) + l; + } + + null_handler = static_cast (e); + t.set ("wrapper-null-handler", null_handler); + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "wrapper_traits specialization does not define the " + << "null_handler constant" << endl; + throw; + } + + // Get the null_default flag. // - c.set ("friend", access_ != 0 && is_friend (c.tree_node (), access_)); + if (null_handler) + { + try + { + tree nh ( + lookup_qualified_name ( + inst, get_identifier ("null_default"), false, false)); - // Assign pointer. + if (nh == error_mark_node || TREE_CODE (nh) != VAR_DECL) + throw operation_failed (); + + // Instantiate this decalaration so that we can get its value. + // + if (DECL_TEMPLATE_INSTANTIATION (nh) && + !DECL_TEMPLATE_INSTANTIATED (nh) && + !DECL_EXPLICIT_INSTANTIATION (nh)) + instantiate_decl (nh, false, false); + + tree init (DECL_INITIAL (nh)); + + if (init == error_mark_node || TREE_CODE (init) != INTEGER_CST) + throw operation_failed (); + + unsigned long long e; + + { + HOST_WIDE_INT hwl (TREE_INT_CST_LOW (init)); + HOST_WIDE_INT hwh (TREE_INT_CST_HIGH (init)); + + unsigned long long l (hwl); + unsigned long long h (hwh); + unsigned short width (HOST_BITS_PER_WIDE_INT); + + e = (h << width) + l; + } + + t.set ("wrapper-null-default", static_cast (e)); + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "wrapper_traits specialization does not define the " + << "null_default constant" << endl; + throw; + } + } + + // Check if the wrapper is a TR1 template instantiation. // - if (k == class_object || k == class_view) - assign_pointer (c); + if (tree ti = TYPE_TEMPLATE_INFO (t.tree_node ())) + { + tree decl (TI_TEMPLATE (ti)); // DECL_TEMPLATE - if (k == class_object) - traverse_object (c); + // Get to the most general template declaration. + // + while (DECL_TEMPLATE_INFO (decl)) + decl = DECL_TI_TEMPLATE (decl); - names (c); + bool& tr1 (features.tr1_pointer); + bool& boost (features.boost_pointer); + + string n (decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); + + // In case of a boost TR1 implementation, we cannot distinguish + // between the boost:: and std::tr1:: usage since the latter is + // just a using-declaration for the former. + // + tr1 = tr1 + || n.compare (0, 8, "std::tr1") == 0 + || n.compare (0, 10, "::std::tr1") == 0; + + boost = boost + || n.compare (0, 17, "boost::shared_ptr") == 0 + || n.compare (0, 19, "::boost::shared_ptr") == 0; + } + + t.set ("wrapper", true); + return true; } // - // Object. + // Process object pointer. // - virtual void - traverse_object (type& c) + semantics::class_* + process_object_pointer (semantics::data_member& m, + semantics::type& t, + string const& kp = string ()) { - semantics::class_* poly_root (polymorphic (c)); - - // Determine whether it is a session object. + // The overall idea is as follows: try to instantiate the pointer + // traits class template. If we are successeful, then get the + // element type and see if it is an object. // - if (!c.count ("session")) + using semantics::class_; + using semantics::data_member; + + class_* c (0); + + if (t.count ("element-type")) + c = t.get ("element-type"); + else { - // If this is a derived class in a polymorphic hierarchy, - // then it should have the same session value as the root. + tree inst (instantiate_template (pointer_traits_, t.tree_node ())); + + if (inst == 0) + return 0; + + // @@ This points to the primary template, not the specialization. // - if (poly_root != 0 && poly_root != &c) - c.set ("session", session (*poly_root)); - else + tree decl (TYPE_NAME (inst)); + + string fl (DECL_SOURCE_FILE (decl)); + size_t ln (DECL_SOURCE_LINE (decl)); + size_t cl (DECL_SOURCE_COLUMN (decl)); + + // Get the element type. + // + tree tn (0); + try { - // See if any of the namespaces containing this class specify - // the session value. + tree decl ( + lookup_qualified_name ( + inst, get_identifier ("element_type"), true, false)); + + if (decl == error_mark_node || TREE_CODE (decl) != TYPE_DECL) + throw operation_failed (); + + tn = TYPE_MAIN_VARIANT (TREE_TYPE (decl)); + + // Check if the pointer is a TR1 template instantiation. // - bool found (false); - for (semantics::scope* s (&c.scope ());; s = &s->scope_ ()) + if (tree ti = TYPE_TEMPLATE_INFO (t.tree_node ())) { - using semantics::namespace_; + decl = TI_TEMPLATE (ti); // DECL_TEMPLATE - namespace_* ns (dynamic_cast (s)); + // Get to the most general template declaration. + // + while (DECL_TEMPLATE_INFO (decl)) + decl = DECL_TI_TEMPLATE (decl); - if (ns == 0) - continue; // Some other scope. + bool& tr1 (features.tr1_pointer); + bool& boost (features.boost_pointer); - if (ns->extension ()) - ns = &ns->original (); + string n (decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); - if (ns->count ("session")) - { - c.set ("session", ns->get ("session")); - found = true; - break; - } + // In case of a boost TR1 implementation, we cannot distinguish + // between the boost:: and std::tr1:: usage since the latter is + // just a using-declaration for the former. + // + tr1 = tr1 + || n.compare (0, 8, "std::tr1") == 0 + || n.compare (0, 10, "::std::tr1") == 0; - if (ns->global_scope ()) - break; + boost = boost + || n.compare (0, 17, "boost::shared_ptr") == 0 + || n.compare (0, 19, "::boost::shared_ptr") == 0; } - - // If still not found, then use the default value. - // - if (!found) - c.set ("session", options.generate_session ()); } - } + catch (operation_failed const&) + { + os << fl << ":" << ln << ":" << cl << ": error: pointer_traits " + << "specialization does not define the 'element_type' type" + << endl; + throw; + } - if (session (c)) - features.session_object = true; + c = dynamic_cast (unit.find (tn)); - if (poly_root != 0) - { - using namespace semantics; + if (c == 0 || !object (*c)) + return 0; - semantics::data_member& idm (*id_member (*poly_root)); + t.set ("element-type", c); - if (poly_root != &c) + // Determine the pointer kind. + // + try { - // If we are a derived class in the polymorphic persistent - // class hierarchy, then add a synthesized virtual pointer - // member that points back to the root. - // - path const& f (idm.file ()); - size_t l (idm.line ()), col (idm.column ()); + tree kind ( + lookup_qualified_name ( + inst, get_identifier ("kind"), false, false)); - semantics::data_member& m ( - unit.new_node (f, l, col, tree (0))); - m.set ("virtual", true); + if (kind == error_mark_node || TREE_CODE (kind) != VAR_DECL) + throw operation_failed (); - // Make it the first member in the class. + // Instantiate this decalaration so that we can get its value. // - node_position np (c, c.names_end ()); - unit.new_edge ( - np, m, idm.name (), access::public_); + if (DECL_TEMPLATE_INSTANTIATION (kind) && + !DECL_TEMPLATE_INSTANTIATED (kind) && + !DECL_EXPLICIT_INSTANTIATION (kind)) + instantiate_decl (kind, false, false); + + tree init (DECL_INITIAL (kind)); + + if (init == error_mark_node || TREE_CODE (init) != INTEGER_CST) + throw operation_failed (); + + unsigned long long e; - // Use the raw pointer as this member's type. - // - if (!poly_root->pointed_p ()) { - // Create the pointer type in the graph. The pointer node - // in GCC seems to always be present, even if not explicitly - // used in the translation unit. - // - tree t (poly_root->tree_node ()); - tree ptr (TYPE_POINTER_TO (t)); - assert (ptr != 0); - ptr = TYPE_MAIN_VARIANT (ptr); - pointer& p (unit.new_node (f, l, col, ptr)); - unit.insert (ptr, p); - unit.new_edge (p, *poly_root); - assert (poly_root->pointed_p ()); - } + HOST_WIDE_INT hwl (TREE_INT_CST_LOW (init)); + HOST_WIDE_INT hwh (TREE_INT_CST_HIGH (init)); - unit.new_edge (m, poly_root->pointed ().pointer ()); + unsigned long long l (hwl); + unsigned long long h (hwh); + unsigned short width (HOST_BITS_PER_WIDE_INT); - // Mark it as a special kind of id. - // - m.set ("id", true); - m.set ("polymorphic-ref", true); + e = (h << width) + l; + } + + pointer_kind_type pk = static_cast (e); + t.set ("pointer-kind", pk); } - else + catch (operation_failed const&) { - // If we are a root of the polymorphic persistent class hierarchy, - // then add a synthesized virtual member for the discriminator. - // Use the location of the polymorphic pragma as the location of + os << fl << ":" << ln << ":" << cl << ": error: pointer_traits " + << "specialization does not define the 'kind' constant" << endl; + throw; + } + + // Get the lazy flag. + // + try + { + tree lazy ( + lookup_qualified_name ( + inst, get_identifier ("lazy"), false, false)); + + if (lazy == error_mark_node || TREE_CODE (lazy) != VAR_DECL) + throw operation_failed (); + + // Instantiate this decalaration so that we can get its value. + // + if (DECL_TEMPLATE_INSTANTIATION (lazy) && + !DECL_TEMPLATE_INSTANTIATED (lazy) && + !DECL_EXPLICIT_INSTANTIATION (lazy)) + instantiate_decl (lazy, false, false); + + tree init (DECL_INITIAL (lazy)); + + if (init == error_mark_node || TREE_CODE (init) != INTEGER_CST) + throw operation_failed (); + + unsigned long long e; + + { + HOST_WIDE_INT hwl (TREE_INT_CST_LOW (init)); + HOST_WIDE_INT hwh (TREE_INT_CST_HIGH (init)); + + unsigned long long l (hwl); + unsigned long long h (hwh); + unsigned short width (HOST_BITS_PER_WIDE_INT); + + e = (h << width) + l; + } + + t.set ("pointer-lazy", static_cast (e)); + } + catch (operation_failed const&) + { + os << fl << ":" << ln << ":" << cl << ": error: pointer_traits " + << "specialization does not define the 'kind' constant" << endl; + throw; + } + } + + // Make sure the pointed-to class is complete. + // + if (!c->complete ()) + { + os << m.file () << ":" << m.line () << ":" << m.column () << ": " + << "error: pointed-to class '" << class_fq_name (*c) << "' " + << "is incomplete" << endl; + + os << c->file () << ":" << c->line () << ":" << c->column () << ": " + << "info: class '" << class_name (*c) << "' is declared here" + << endl; + + os << c->file () << ":" << c->line () << ":" << c->column () << ": " + << "info: consider including its definition with the " + << "--odb-epilogue option" << endl; + + throw operation_failed (); + } + + // Make sure the pointed-to class is not reuse-abstract. + // + if (abstract (*c) && !polymorphic (*c)) + { + os << m.file () << ":" << m.line () << ":" << m.column () << ": " + << "error: pointed-to class '" << class_fq_name (*c) << "' " + << "is abstract" << endl; + + os << c->file () << ":" << c->line () << ":" << c->column () << ": " + << "info: class '" << class_name (*c) << "' is defined here" + << endl; + + throw operation_failed (); + } + + // Make sure the pointed-to class has object id. + // + if (id_member (*c) == 0) + { + os << m.file () << ":" << m.line () << ":" << m.column () << ": " + << "error: pointed-to class '" << class_fq_name (*c) << "' " + << "has no object id" << endl; + + os << c->file () << ":" << c->line () << ":" << c->column () << ": " + << "info: class '" << class_name (*c) << "' is defined here" + << endl; + + throw operation_failed (); + } + + // See if this is the inverse side of a bidirectional relationship. + // If so, then resolve the member and cache it in the context. + // + if (m.count ("inverse")) + { + string name (m.get ("inverse")); + location_t loc (m.get ("inverse-location")); + + try + { + data_member& im ( + c->lookup (name, class_::include_hidden)); + + // @@ Would be good to check that the other end is actually + // an object pointer, is not marked as transient or inverse, + // and points to the correct object. But the other class may + // not have been processed yet. + // + m.remove ("inverse"); + m.set (kp + (kp.empty () ? "": "-") + "inverse", &im); + } + catch (semantics::unresolved const& e) + { + if (e.type_mismatch) + error (loc) << "name '" << name << "' in '#pragma db " << + "inverse' does not refer to a data member" << endl; + else + error (loc) << "unable to resolve data member '" << name << + "' specified with '#pragma db inverse'" << endl; + + throw operation_failed (); + } + catch (semantics::ambiguous const& e) + { + error (loc) << "data member name '" << name << "' specified " << + "with '#pragma db inverse' is ambiguous" << endl; + + info (e.first.named ().location ()) << "could resolve to this " << + "data member" << endl; + + info (e.second.named ().location ()) << "or could resolve to " << + "this data member" << endl; + + throw operation_failed (); + } + } + + return c; + } + + // + // Process container. + // + + void + process_container_value (semantics::type& t, + semantics::data_member& m, + string const& prefix, + bool obj_ptr) + { + process_wrapper (t); + + if (composite_wrapper (t)) + return; + + if (obj_ptr) + process_object_pointer (m, t, prefix); + } + + bool + process_container (semantics::data_member& m, semantics::type& t) + { + // The overall idea is as follows: try to instantiate the container + // traits class template. If we are successeful, then this is a + // container type and we can extract the various information from + // the instantiation. Otherwise, this is not a container. + // + + container_kind_type ck; + semantics::type* vt (0); + semantics::type* it (0); + semantics::type* kt (0); + + semantics::names* vh (0); + semantics::names* ih (0); + semantics::names* kh (0); + + if (t.count ("container-kind")) + { + ck = t.get ("container-kind"); + vt = t.get ("value-tree-type"); + vh = t.get ("value-tree-hint"); + + if (ck == ck_ordered) + { + it = t.get ("index-tree-type"); + ih = t.get ("index-tree-hint"); + } + + if (ck == ck_map || ck == ck_multimap) + { + kt = t.get ("key-tree-type"); + kh = t.get ("key-tree-hint"); + } + } + else + { + tree inst (instantiate_template (container_traits_, t.tree_node ())); + + if (inst == 0) + return false; + + // @@ This points to the primary template, not the specialization. + // + tree decl (TYPE_NAME (inst)); + + string f (DECL_SOURCE_FILE (decl)); + size_t l (DECL_SOURCE_LINE (decl)); + size_t c (DECL_SOURCE_COLUMN (decl)); + + // Determine the container kind. + // + try + { + tree kind ( + lookup_qualified_name ( + inst, get_identifier ("kind"), false, false)); + + if (kind == error_mark_node || TREE_CODE (kind) != VAR_DECL) + throw operation_failed (); + + + // Instantiate this decalaration so that we can get its value. + // + if (DECL_TEMPLATE_INSTANTIATION (kind) && + !DECL_TEMPLATE_INSTANTIATED (kind) && + !DECL_EXPLICIT_INSTANTIATION (kind)) + instantiate_decl (kind, false, false); + + tree init (DECL_INITIAL (kind)); + + if (init == error_mark_node || TREE_CODE (init) != INTEGER_CST) + throw operation_failed (); + + unsigned long long e; + + { + HOST_WIDE_INT hwl (TREE_INT_CST_LOW (init)); + HOST_WIDE_INT hwh (TREE_INT_CST_HIGH (init)); + + unsigned long long l (hwl); + unsigned long long h (hwh); + unsigned short width (HOST_BITS_PER_WIDE_INT); + + e = (h << width) + l; + } + + ck = static_cast (e); + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "container_traits specialization does not define the " + << "container kind constant" << endl; + + throw; + } + + t.set ("container-kind", ck); + + // Mark id column as not null. + // + t.set ("id-not-null", true); + + // Get the value type. + // + try + { + tree decl ( + lookup_qualified_name ( + inst, get_identifier ("value_type"), true, false)); + + if (decl == error_mark_node || TREE_CODE (decl) != TYPE_DECL) + throw operation_failed (); + + tree type (TYPE_MAIN_VARIANT (TREE_TYPE (decl))); + vt = &dynamic_cast (*unit.find (type)); + + // Find the hint. + // + for (tree ot (DECL_ORIGINAL_TYPE (decl)); + ot != 0; + ot = decl ? DECL_ORIGINAL_TYPE (decl) : 0) + { + if ((vh = unit.find_hint (ot))) + break; + + decl = TYPE_NAME (ot); + } + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "container_traits specialization does not define the " + << "value_type type" << endl; + + throw; + } + + t.set ("value-tree-type", vt); + t.set ("value-tree-hint", vh); + + // If we have a set container, automatically mark the value + // column as not null. If we already have an explicit null for + // this column, issue an error. + // + if (ck == ck_set) + { + if (t.count ("value-null")) + { + os << t.file () << ":" << t.line () << ":" << t.column () << ":" + << " error: set container cannot contain null values" << endl; + + throw operation_failed (); + } + else + t.set ("value-not-null", true); + } + + // Issue a warning if we are relaxing null-ness in the + // container type. + // + if (t.count ("value-null") && vt->count ("not-null")) + { + os << t.file () << ":" << t.line () << ":" << t.column () << ":" + << " warning: container value declared null while its type " + << "is declared not null" << endl; + } + + // Get the index type for ordered containers. + // + if (ck == ck_ordered) + { + try + { + tree decl ( + lookup_qualified_name ( + inst, get_identifier ("index_type"), true, false)); + + if (decl == error_mark_node || TREE_CODE (decl) != TYPE_DECL) + throw operation_failed (); + + tree type (TYPE_MAIN_VARIANT (TREE_TYPE (decl))); + it = &dynamic_cast (*unit.find (type)); + + // Find the hint. + // + for (tree ot (DECL_ORIGINAL_TYPE (decl)); + ot != 0; + ot = decl ? DECL_ORIGINAL_TYPE (decl) : 0) + { + if ((ih = unit.find_hint (ot))) + break; + + decl = TYPE_NAME (ot); + } + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "container_traits specialization does not define the " + << "index_type type" << endl; + + throw; + } + + t.set ("index-tree-type", it); + t.set ("index-tree-hint", ih); + t.set ("index-not-null", true); + } + + // Get the key type for maps. + // + if (ck == ck_map || ck == ck_multimap) + { + try + { + tree decl ( + lookup_qualified_name ( + inst, get_identifier ("key_type"), true, false)); + + if (decl == error_mark_node || TREE_CODE (decl) != TYPE_DECL) + throw operation_failed (); + + tree type (TYPE_MAIN_VARIANT (TREE_TYPE (decl))); + kt = &dynamic_cast (*unit.find (type)); + + // Find the hint. + // + for (tree ot (DECL_ORIGINAL_TYPE (decl)); + ot != 0; + ot = decl ? DECL_ORIGINAL_TYPE (decl) : 0) + { + if ((kh = unit.find_hint (ot))) + break; + + decl = TYPE_NAME (ot); + } + } + catch (operation_failed const&) + { + os << f << ":" << l << ":" << c << ": error: " + << "container_traits specialization does not define the " + << "key_type type" << endl; + + throw; + } + + t.set ("key-tree-type", kt); + t.set ("key-tree-hint", kh); + t.set ("key-not-null", true); + } + } + + // Process member data. + // + m.set ("id-tree-type", &id_tree_type); + + process_container_value (*vt, m, "value", true); + + if (it != 0) + process_container_value (*it, m, "index", false); + + if (kt != 0) + process_container_value (*kt, m, "key", false); + + // If this is an inverse side of a bidirectional object relationship + // and it is an ordered container, mark it as unordred since there is + // no concept of order in this construct. + // + if (ck == ck_ordered && m.count ("value-inverse")) + m.set ("unordered", true); + + // Issue an error if we have a null column in a set container. + // This can only happen if the value is declared as null in + // the member. + // + if (ck == ck_set && m.count ("value-null")) + { + os << m.file () << ":" << m.line () << ":" << m.column () << ":" + << " error: set container cannot contain null values" << endl; + + throw operation_failed (); + } + + // Issue a warning if we are relaxing null-ness in the member. + // + if (m.count ("value-null") && + (t.count ("value-not-null") || vt->count ("not-null"))) + { + os << m.file () << ":" << m.line () << ":" << m.column () << ":" + << " warning: container value declared null while the container " + << "type or value type declares it as not null" << endl; + } + + return true; + } + + // + // Implementation details (c-tor, helpers). + // + + data_member () + { + // Find the odb namespace. + // + tree odb = lookup_qualified_name ( + global_namespace, get_identifier ("odb"), false, false); + + if (odb == error_mark_node) + { + os << unit.file () << ": error: unable to resolve odb namespace" + << endl; + + throw operation_failed (); + } + + // Find wrapper traits. + // + wrapper_traits_ = lookup_qualified_name ( + odb, get_identifier ("wrapper_traits"), true, false); + + if (wrapper_traits_ == error_mark_node || + !DECL_CLASS_TEMPLATE_P (wrapper_traits_)) + { + os << unit.file () << ": error: unable to resolve wrapper_traits " + << "in the odb namespace" << endl; + + throw operation_failed (); + } + + // Find pointer traits. + // + pointer_traits_ = lookup_qualified_name ( + odb, get_identifier ("pointer_traits"), true, false); + + if (pointer_traits_ == error_mark_node || + !DECL_CLASS_TEMPLATE_P (pointer_traits_)) + { + os << unit.file () << ": error: unable to resolve pointer_traits " + << "in the odb namespace" << endl; + + throw operation_failed (); + } + + // Find the access class. + // + tree access = lookup_qualified_name ( + odb, get_identifier ("access"), true, false); + + if (access == error_mark_node) + { + os << unit.file () << ": error: unable to resolve access class" + << "in the odb namespace" << endl; + + throw operation_failed (); + } + + access = TREE_TYPE (access); + + // Find container_traits. + // + container_traits_ = lookup_qualified_name ( + access, get_identifier ("container_traits"), true, false); + + if (container_traits_ == error_mark_node || + !DECL_CLASS_TEMPLATE_P (container_traits_)) + { + os << unit.file () << ": error: unable to resolve container_traits " + << "in the odb namespace" << endl; + + throw operation_failed (); + } + } + + static tree + instantiate_template (tree t, tree arg) + { + tree args (make_tree_vec (1)); + TREE_VEC_ELT (args, 0) = arg; + + // This step should succeed regardles of whether there is a + // specialization for this type. + // + tree inst ( + lookup_template_class (t, args, 0, 0, 0, tf_warning_or_error)); + + if (inst == error_mark_node) + { + // Diagnostics has already been issued by lookup_template_class. + // + throw operation_failed (); + } + + inst = TYPE_MAIN_VARIANT (inst); + + // The instantiation may already be complete if it matches a + // (complete) specialization or was used before. + // + if (!COMPLETE_TYPE_P (inst)) + inst = instantiate_class_template (inst); + + // If we cannot instantiate this type, assume there is no suitable + // specialization for it. + // + if (inst == error_mark_node || !COMPLETE_TYPE_P (inst)) + return 0; + + return inst; + } + + private: + tree wrapper_traits_; + tree pointer_traits_; + tree container_traits_; + }; + + struct class_: traversal::class_, context + { + class_ () + : std_string_ (0), std_string_hint_ (0), access_ (0) + { + *this >> member_names_ >> member_; + + // Resolve the std::string type node. + // + using semantics::scope; + + for (scope::names_iterator_pair ip (unit.find ("std")); + ip.first != ip.second; ++ip.first) + { + if (scope* ns = dynamic_cast (&ip.first->named ())) + { + scope::names_iterator_pair jp (ns->find ("string")); + + if (jp.first != jp.second) + { + std_string_ = dynamic_cast ( + &jp.first->named ()); + std_string_hint_ = &*jp.first; + break; + } + } + } + + assert (std_string_ != 0); // No std::string? + + // Resolve odb::access, if any. + // + tree odb = lookup_qualified_name ( + global_namespace, get_identifier ("odb"), false, false); + + if (odb != error_mark_node) + { + access_ = lookup_qualified_name ( + odb, get_identifier ("access"), true, false); + + access_ = (access_ != error_mark_node ? TREE_TYPE (access_) : 0); + } + } + + virtual void + traverse (type& c) + { + class_kind_type k (class_kind (c)); + + if (k == class_other) + return; + + // Check if odb::access is a friend of this class. + // + c.set ("friend", access_ != 0 && is_friend (c.tree_node (), access_)); + + // Assign pointer. + // + if (k == class_object || k == class_view) + assign_pointer (c); + + if (k == class_object) + traverse_object (c); + else if (k == class_view) + traverse_view (c); + + names (c); + } + + // + // Object. + // + + virtual void + traverse_object (type& c) + { + semantics::class_* poly_root (polymorphic (c)); + + // Determine whether it is a session object. + // + if (!c.count ("session")) + { + // If this is a derived class in a polymorphic hierarchy, + // then it should have the same session value as the root. + // + if (poly_root != 0 && poly_root != &c) + c.set ("session", session (*poly_root)); + else + { + // See if any of the namespaces containing this class specify + // the session value. + // + bool found (false); + for (semantics::scope* s (&c.scope ());; s = &s->scope_ ()) + { + using semantics::namespace_; + + namespace_* ns (dynamic_cast (s)); + + if (ns == 0) + continue; // Some other scope. + + if (ns->extension ()) + ns = &ns->original (); + + if (ns->count ("session")) + { + c.set ("session", ns->get ("session")); + found = true; + break; + } + + if (ns->global_scope ()) + break; + } + + // If still not found, then use the default value. + // + if (!found) + c.set ("session", options.generate_session ()); + } + } + + if (session (c)) + features.session_object = true; + + if (poly_root != 0) + { + using namespace semantics; + + semantics::data_member& idm (*id_member (*poly_root)); + + if (poly_root != &c) + { + // If we are a derived class in the polymorphic persistent + // class hierarchy, then add a synthesized virtual pointer + // member that points back to the root. + // + path const& f (idm.file ()); + size_t l (idm.line ()), col (idm.column ()); + + semantics::data_member& m ( + unit.new_node (f, l, col, tree (0))); + m.set ("virtual", true); + + // Make it the first member in the class. + // + node_position np (c, c.names_end ()); + unit.new_edge ( + np, m, idm.name (), access::public_); + + // Use the raw pointer as this member's type. + // + if (!poly_root->pointed_p ()) + { + // Create the pointer type in the graph. The pointer node + // in GCC seems to always be present, even if not explicitly + // used in the translation unit. + // + tree t (poly_root->tree_node ()); + tree ptr (TYPE_POINTER_TO (t)); + assert (ptr != 0); + ptr = TYPE_MAIN_VARIANT (ptr); + pointer& p (unit.new_node (f, l, col, ptr)); + unit.insert (ptr, p); + unit.new_edge (p, *poly_root); + assert (poly_root->pointed_p ()); + } + + unit.new_edge (m, poly_root->pointed ().pointer ()); + + // Mark it as a special kind of id. + // + m.set ("id", true); + m.set ("polymorphic-ref", true); + } + else + { + // If we are a root of the polymorphic persistent class hierarchy, + // then add a synthesized virtual member for the discriminator. + // Use the location of the polymorphic pragma as the location of // this member. // location_t loc (c.get ("polymorphic-location")); @@ -729,6 +1726,124 @@ namespace } } + // + // View. + // + + virtual void + traverse_view (type& c) + { + // Resolve referenced objects from tree nodes to semantic graph + // nodes. Also populate maps and compute counts. + // + view_alias_map& amap (c.set ("alias-map", view_alias_map ())); + view_object_map& omap (c.set ("object-map", view_object_map ())); + + size_t& obj_count (c.set ("object-count", size_t (0))); + size_t& tbl_count (c.set ("table-count", size_t (0))); + + if (c.count ("objects")) + { + using semantics::class_; + + view_objects& objs (c.get ("objects")); + + for (view_objects::iterator i (objs.begin ()); i != objs.end (); ++i) + { + if (i->kind != view_object::object) + { + tbl_count++; + continue; + } + else + obj_count++; + + tree n (TYPE_MAIN_VARIANT (i->obj_node)); + + if (TREE_CODE (n) != RECORD_TYPE) + { + error (i->loc) << "name '" << i->obj_name << "' in db pragma " << + "object does not name a class" << endl; + + throw operation_failed (); + } + + class_& o (dynamic_cast (*unit.find (n))); + + if (!object (o)) + { + error (i->loc) << "name '" << i->obj_name << "' in db pragma " << + "object does not name a persistent class" << endl; + + info (o.location ()) << "class '" << i->obj_name << "' is " << + "defined here" << endl; + + throw operation_failed (); + } + + i->obj = &o; + + if (i->alias.empty ()) + { + if (!omap.insert (view_object_map::value_type (&o, &*i)).second) + { + error (i->loc) << "persistent class '" << i->obj_name << + "' is used in the view more than once" << endl; + + error (omap[&o]->loc) << "previously used here" << endl; + + info (i->loc) << "use the alias clause to assign it a " << + "different name" << endl; + + throw operation_failed (); + } + + // Also add the bases of a polymorphic object. + // + class_* poly_root (polymorphic (o)); + + if (poly_root != 0 && poly_root != &o) + { + for (class_* b (&polymorphic_base (o));; + b = &polymorphic_base (*b)) + { + if (!omap.insert (view_object_map::value_type (b, &*i)).second) + { + error (i->loc) << "base class '" << class_name (*b) << + "' is used in the view more than once" << endl; + + error (omap[b]->loc) << "previously used here" << endl; + + info (i->loc) << "use the alias clause to assign it a " << + "different name" << endl; + + throw operation_failed (); + } + + if (b == poly_root) + break; + } + } + } + else + { + if (!amap.insert ( + view_alias_map::value_type (i->alias, &*i)).second) + { + error (i->loc) << "alias '" << i->alias << "' is used in " << + "the view more than once" << endl; + + throw operation_failed (); + } + } + } + } + } + + // + // Assign object/view pointer. + // + void assign_pointer (type& c) { -- cgit v1.1