aboutsummaryrefslogtreecommitdiff
path: root/odb/lookup.cxx
blob: b3e0551343516f4f1ef8391ff6be626bae61311e (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
// file      : odb/lookup.cxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/lookup.hxx>

using namespace std;

namespace lookup
{
  std::string
  parse_scoped_name (cxx_lexer& l, cpp_ttype& tt, string& tl, tree& tn)
  {
    string name;

    if (tt == CPP_SCOPE)
    {
      name += "::";
      tt = l.next (tl, &tn);
    }

    while (true)
    {
      // @@ We still need to handle fundamental types, e.g., unsigned int.
      //
      if (tt != CPP_NAME && tt != CPP_KEYWORD)
        throw invalid_name ();

      name += tl;
      tt = l.next (tl, &tn);

      if (tt != CPP_SCOPE)
        break;

      name += "::";
      tt = l.next (tl, &tn);
    }

    return name;
  }

  tree
  resolve_scoped_name (cxx_lexer& l,
                       cpp_ttype& tt,
                       string& tl,
                       tree& tn,
                       cpp_ttype& ptt,
                       tree scope,
                       string& name,
                       bool is_type,
                       bool trailing_scope,
                       tree* end_scope)
  {
    tree id;
    bool first (true);

    if (tt == CPP_SCOPE)
    {
      name += "::";
      scope = global_namespace;
      first = false;

      ptt = tt;
      tt = l.next (tl, &tn);
    }

    while (true)
    {
      if (end_scope != 0)
        *end_scope = scope;

      // @@ We still need to handle fundamental types, e.g., unsigned int.
      //
      if (tt != CPP_NAME && tt != CPP_KEYWORD)
        throw invalid_name ();

      name += tl;
      id = get_identifier (tl.c_str ());
      ptt = tt;
      tt = l.next (tl, &tn);

      bool last (true);
      if (tt == CPP_SCOPE)
      {
        // If trailing scope names are allowed, then we also need to
        // check what's after the scope.
        //
        if (trailing_scope)
        {
          ptt = tt;
          tt = l.next (tl, &tn);

          if (tt == CPP_NAME)
            last = false;
        }
        else
          last = false;
      }

      tree decl = lookup_qualified_name (scope, id, last && is_type, false);

      // If this is the first component in the name, then also search the
      // outer scopes.
      //
      if (decl == error_mark_node && first && scope != global_namespace)
      {
        do
        {
          scope = TYPE_P (scope)
            ? CP_TYPE_CONTEXT (scope)
            : CP_DECL_CONTEXT (scope);
          decl = lookup_qualified_name (scope, id, last && is_type, false);
        } while (decl == error_mark_node && scope != global_namespace);
      }

      if (decl == error_mark_node)
        throw unable_to_resolve (name, last);

      scope = decl;

      if (last)
        break;

      first = false;

      if (TREE_CODE (scope) == TYPE_DECL)
        scope = TREE_TYPE (scope);

      name += "::";

      if (!trailing_scope)
      {
        ptt = tt;
        tt = l.next (tl, &tn);
      }
    }

    return scope;
  }
}