summaryrefslogtreecommitdiff
path: root/cli/semantics/unit.hxx
blob: 76a2acd6a53626cfd827aac5b2c6dd63d2b7aead (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// file      : cli/semantics/unit.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CLI_SEMANTICS_UNIT_HXX
#define CLI_SEMANTICS_UNIT_HXX

#include <map>
#include <vector>
#include <string>

#include <cli/semantics/elements.hxx>
#include <cli/semantics/namespace.hxx>

namespace semantics
{
  //
  //
  class cli_unit;
  class cxx_unit;

  //
  //
  class includes: public edge
  {
  public:
    enum kind_type {quote, bracket};

    cli_unit&
    includer () const
    {
      return *includer_;
    }

    kind_type
    kind () const
    {
      return kind_;
    }

    path const&
    file () const
    {
      return file_;
    }

  public:
    includes (kind_type kind, path const& file)
        : kind_ (kind), file_ (file)
    {
    }

    void
    set_left_node (cli_unit& n)
    {
      includer_ = &n;
    }

  protected:
    kind_type kind_;
    path file_;
    cli_unit* includer_;
  };

  //
  //
  class cli_includes: public includes
  {
  public:
    cli_unit&
    includee () const
    {
      return *includee_;
    }

  public:
    cli_includes (kind_type kind, path const& file)
        : includes (kind, file)
    {
    }

    void
    set_right_node (cli_unit& n)
    {
      includee_ = &n;
    }

  private:
    cli_unit* includee_;
  };

  //
  //
  class cxx_includes: public includes
  {
  public:
    cxx_unit&
    includee () const
    {
      return *includee_;
    }

  public:
    cxx_includes (kind_type kind, path const& file)
        : includes (kind, file)
    {
    }

    void
    set_right_node (cxx_unit& n)
    {
      includee_ = &n;
    }

  private:
    cxx_unit* includee_;
  };

  //
  //
  class cxx_unit: public node
  {
  public:
    cxx_unit (path const& file, size_t line, size_t column)
        : node (file, line, column)
    {
    }

    void
    add_edge_right (cxx_includes&)
    {
    }
  };

  //
  //
  class cli_unit: public graph<node, edge>, public namespace_
  {
    typedef std::vector<includes*> includes_list;

  public:
    // Lookup a name in the specified starting scope. Empty scope denotes
    // the global namespace. Starting scope should be a fully-qualified
    // name while name can be qualified but should not be fully-qualified
    // (to lookup a fully-qualified name use the global namespace as the
    // starting scope).
    //
    // The lookup starts in this unit and continues in all the units that
    // this unit includes, transitively.
    //
    // The outer flag specifies whether to search the outer scopes.
    //
    template <typename T>
    T*
    lookup (std::string const& scope,
            std::string const& name,
            bool outer = true);

  public:
    typedef
    pointer_iterator<includes_list::const_iterator>
    includes_iterator;

    includes_iterator
    includes_begin () const
    {
      return includes_.begin ();
    }

    includes_iterator
    includes_end () const
    {
      return includes_.end ();
    }

  public:
    cli_unit (path const& file, size_t line, size_t column)
        : node (file, line, column), graph_ (*this)
    {
      // Use a special edge to get this->name() return the global
      // namespace name ("").
      //
      new_edge<global_names> (*this, *this);
    }

  public:
    template <typename T>
    T&
    new_node (path const& file, size_t line, size_t column)
    {
      return graph_.new_node<T> (file, line, column);
    }

    template <typename T, typename A0>
    T&
    new_node (path const& file, size_t line, size_t column, A0 const& a0)
    {
      return graph_.new_node<T> (file, line, column, a0);
    }

    template <typename T, typename A0, typename A1>
    T&
    new_node (path const& file, size_t line, size_t column,
              A0 const& a0, A1 const& a1)
    {
      return graph_.new_node<T> (file, line, column, a0, a1);
    }

    template <typename T, typename A0, typename A1, typename A2>
    T&
    new_node (path const& file, size_t line, size_t column,
              A0 const& a0, A1 const& a1, A2 const& a2)
    {
      return graph_.new_node<T> (file, line, column, a0, a1, a2);
    }

    template <typename T, typename A0, typename A1, typename A2, typename A3>
    T&
    new_node (path const& file, size_t line, size_t column,
              A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3)
    {
      return graph_.new_node<T> (file, line, column, a0, a1, a2, a3);
    }

  public:
    type&
    new_type (path const& file, size_t l, size_t c, string const& name)
    {
      type_map::iterator i (types_.find (name));

      return i != types_.end ()
        ? *i->second
        : *(types_[name] = &new_node<type> (file, l, c, name));
    }

  protected:
    // Special names edge for the global namespace.
    //
    class global_names: public names
    {
    public:
      global_names ()
          : names ("")
      {
        scope_ = 0;
      }

      void
      set_left_node (cli_unit&)
      {
      }

      void
      set_right_node (nameable& n)
      {
        named_ = &n;
      }

      void
      clear_left_node (cli_unit&)
      {
      }

      void
      clear_right_node (nameable& n)
      {
        assert (named_ == &n);
        named_ = 0;
      }
    };

  public:
    void
    add_edge_left (cli_includes& e)
    {
      includes_.push_back (&e);
    }

    void
    add_edge_left (cxx_includes& e)
    {
      includes_.push_back (&e);
    }

    void
    add_edge_left (global_names&)
    {
    }

    void
    add_edge_right (cli_includes&)
    {
    }

    using namespace_::add_edge_left;
    using namespace_::add_edge_right;

  private:
    typedef std::map<string, type*> type_map;

  private:
    graph<node, edge>& graph_;
    includes_list includes_;
    type_map types_;
  };
}

#include <cli/semantics/unit.txx>

#endif // CLI_SEMANTICS_UNIT_HXX