aboutsummaryrefslogtreecommitdiff
path: root/odb/semantics/class.cxx
blob: c1607abc6ac89542c9dac10331dfe2a75e104552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// file      : odb/semantics/class.cxx
// copyright : Copyright (c) 2009-2018 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/gcc.hxx> // TYPE_HAS_DEFAULT_CONSTRUCTOR

#include <cutl/compiler/type-info.hxx>
#include <odb/semantics/class.hxx>

namespace semantics
{
  inherits::
  inherits (access_type access, bool virt)
      : virt_ (virt), access_ (access)
  {
  }

  class_::
  class_ (path const& file, size_t line, size_t column, tree tn)
      : node (file, line, column, tn)
  {
  }

  bool class_::
  default_ctor () const
  {
    tree t (tree_node ());

    // TYPE_HAS_DEFAULT_CONSTRUCTOR() returns true if we have a
    // deleted default ctor. locate_ctor(), on the other hand,
    // returns NULL_TREE in this case.
    //
    return TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && locate_ctor (t) != NULL_TREE;
  }

  bool class_::
  complete () const
  {
    return COMPLETE_TYPE_P (tree_node ());
  }

  bool class_::
  abstract () const
  {
    return CLASSTYPE_PURE_VIRTUALS (tree_node ());
  }

  names* class_::
  lookup (string const& name,
          type_id const& ti,
          unsigned int flags,
          bool* ph) const
  {
    bool h (false);
    bool& rh (ph != 0 ? *ph : h);

    names* r (scope::lookup (name, ti, flags | exclude_outer, &rh));

    if (r != 0)
      return r;

    // If we found a name but the types didn't match, then bail out
    // unless we want hidden names.
    //
    if (rh && (flags & include_hidden) == 0)
      return 0;

    // Look in the base classes unless requested not to. For the name
    // lookup purposes, bases can be viewed as a parallel set of outer
    // scopes that are searched after the class scope and before any
    // real outer scope. Interestingly, outer scopes of bases are not
    // considered during this lookup, only their bases.
    //
    if ((flags & exclude_base) == 0)
    {
      // Being hidden in one base doesn't mean it is also hidden in the
      // other. Normally that would be an ambiguous lookup, but we use
      // relaxed rules.
      //
      bool any_h (false); // Indicates whether any base hides the name.

      for (inherits_iterator i (inherits_begin ()); i != inherits_end (); ++i)
      {
        bool h (false); // Indicates whether this base hides the name.
        names* br (i->base ().lookup (name, ti, flags | exclude_outer, &h));
        any_h = any_h || h;

        if (br != 0)
        {
          if (r != 0)
            throw ambiguous (*r, *br);

          r = br;

          if (h)
            rh = true;
        }
      }

      if (r != 0)
        return r;

      if (any_h)
      {
        rh = true;
        if ((flags & include_hidden) == 0)
          return 0;
      }
    }

    // Look in the outer scope unless requested not to.
    //
    if ((flags & exclude_outer) == 0)
      return scope ().lookup (name, ti, flags, &rh);

    return 0;
  }

  // type info
  //
  namespace
  {
    struct init
    {
      init ()
      {
        using compiler::type_info;

        // inherits
        //
        {
          type_info ti (typeid (inherits));
          ti.add_base (typeid (edge));
          insert (ti);
        }

        // class_
        //
        {
          type_info ti (typeid (class_));
          ti.add_base (typeid (type));
          ti.add_base (typeid (scope));
          insert (ti);
        }
      }
    } init_;
  }
}