summaryrefslogtreecommitdiff
path: root/odb/type-processor.cxx
blob: b99cfb9bf30ef8f0942b00a9a80293c944413fa5 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// file      : odb/type-processor.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/gcc.hxx>

#include <odb/type-processor.hxx>

namespace
{
  struct data_member: traversal::data_member, context
  {
    data_member (context& c)
        : context (c)
    {
      // 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 generation_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 generation_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 generation_failed ();
      }
    }

    virtual void
    traverse (semantics::data_member& m)
    {
      if (m.count ("transient"))
        return;

      semantics::type& t (m.type ());

      // Nothing to do if this is a composite value type.
      //
      if (comp_value (t))
        return;

      string type;

      if (m.count ("type"))
        type = m.get<string> ("type");

      if (type.empty () && t.count ("type"))
        type = t.get<string> ("type");

      type = column_type_impl (t, type, &m);

      if (!type.empty ())
      {
        m.set ("column-type", type);
        return;
      }

      // See if this is a container type.
      //
      if (process_container (m))
        return;

      // If it is none of the above then we have an error.
      //
      string const& fq_type (t.fq_name (m.belongs ().hint ()));

      os << m.file () << ":" << m.line () << ":" << m.column () << ":"
         << " error: unable to map C++ type '" << fq_type << "' used in "
         << "data member '" << m.name () << "' to a database type" << endl;

      os << m.file () << ":" << m.line () << ":" << m.column () << ":"
         << " info: use '#pragma db type' to specify the database type"
         << endl;

      throw generation_failed ();
    }

    void
    process_container_value (semantics::type& t,
                             string const& prefix,
                             semantics::data_member& m)
    {
      if (comp_value (t))
        return;

      string type;

      // Custom mapping can come from these places (listed in the order
      // of priority): member, container type, value type.
      //
      if (m.count (prefix + "-type"))
        type = m.get<string> (prefix + "-type");

      semantics::type& ct (m.type ());

      if (type.empty () && ct.count (prefix + "-type"))
        type = ct.get<string> (prefix + "-type");

      if (type.empty () && t.count ("type"))
        type = t.get<string> ("type");

      type = column_type_impl (t, type, 0);

      if (!type.empty ())
      {
        m.set (prefix + "-column-type", type);
        return;
      }

      // We do not support nested containers so skip that test.
      //

      // If it is none of the above then we have an error.
      //
      string fq_type (t.fq_anonymous () ? "<anonymous>" : t.fq_name ());

      os << m.file () << ":" << m.line () << ":" << m.column () << ":"
         << " error: unable to map C++ type '" << fq_type << "' used in "
         << "data member '" << m.name () << "' to a database type" << endl;

      os << m.file () << ":" << m.line () << ":" << m.column () << ":"
         << " info: use '#pragma db " << prefix << "_type' to specify the "
         << "database type" << endl;

      throw generation_failed ();
    }

    bool
    process_container (semantics::data_member& m)
    {
      // 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.
      //

      semantics::type& t (m.type ());

      container_kind_type ck;
      semantics::type* vt (0);
      semantics::type* it (0);
      semantics::type* kt (0);

      if (t.count ("container"))
      {
        ck = t.get<container_kind_type> ("container-kind");
        vt = t.get<semantics::type*> ("tree-value-type");

        if (ck == ck_ordered)
          it = t.get<semantics::type*> ("tree-index-type");

        if (ck == ck_map || ck == ck_multimap)
          kt = t.get<semantics::type*> ("tree-key-type");
      }
      else
      {
        tree args (make_tree_vec (1));
        TREE_VEC_ELT (args, 0) = t.tree_node ();

        // This step should succeed regardles of whether there is a
        // container traits specialization for this type.
        //
        tree inst (
          lookup_template_class (
          container_traits_, args, 0, 0, 0, tf_warning_or_error));

        if (inst == error_mark_node)
        {
          // Diagnostics has already been issued by lookup_template_class.
          //
          throw generation_failed ();
        }

        inst = TYPE_MAIN_VARIANT (inst);

        // The instantiation may already be complete if it matches a
        // (complete) specialization.
        //
        if (!COMPLETE_TYPE_P (inst))
          inst = instantiate_class_template (inst);

        // If we cannot instantiate this type, assume there is no suitable
        // container traits specialization for it.
        //
        if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
          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 generation_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 generation_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<container_kind_type> (e);
        }
        catch (generation_failed const&)
        {
          os << f << ":" << l << ":" << c << ": error: "
             << "odb::container_traits specialization does not define the "
             << "container kind constant" << endl;

          throw;
        }

        t.set ("container-kind", ck);

        // 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 generation_failed ();

          tree type (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));

          vt = &dynamic_cast<semantics::type&> (*unit.find (type));
        }
        catch (generation_failed const&)
        {
          os << f << ":" << l << ":" << c << ": error: "
             << "odb::container_traits specialization does not define the "
             << "value_type type" << endl;

          throw;
        }

        t.set ("tree-value-type", vt);


        // 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 generation_failed ();

            tree type (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));

            it = &dynamic_cast<semantics::type&> (*unit.find (type));
          }
          catch (generation_failed const&)
          {
            os << f << ":" << l << ":" << c << ": error: "
               << "odb::container_traits specialization does not define the "
               << "index_type type" << endl;

            throw;
          }

          t.set ("tree-index-type", it);
        }

        // 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 generation_failed ();

            tree type (TYPE_MAIN_VARIANT (TREE_TYPE (decl)));

            kt = &dynamic_cast<semantics::type&> (*unit.find (type));
          }
          catch (generation_failed const&)
          {
            os << f << ":" << l << ":" << c << ": error: "
               << "odb::container_traits specialization does not define the "
               << "key_type type" << endl;

            throw;
          }

          t.set ("tree-key-type", kt);
        }
      }

      // Process member data.
      //
      process_container_value (*vt, "value", m);

      if (it != 0)
        process_container_value (*it, "index", m);

      if (kt != 0)
        process_container_value (*kt, "key", m);

      return true;
    }

  private:
    tree container_traits_;
  };

  struct class_: traversal::class_, context
  {
    class_ (context& c)
        : context (c), member_ (c)
    {
      *this >> member_names_ >> member_;
    }

    virtual void
    traverse (type& c)
    {
      bool obj (c.count ("object"));

      if (!(obj || comp_value (c)))
        return;

      names (c);

      // Assign object pointer.
      //
      if (obj)
      {
        string ptr;
        string const& name (c.fq_name ());

        if (c.count ("pointer"))
        {
          string const& p (c.get<string> ("pointer"));

          if (p == "*")
            ptr = name + "*";
          else if (p[p.size () - 1] == '*')
            ptr = p;
          else if (p.find ('<') != string::npos)
            ptr = p;
          else
          {
            // This is not a template-id. Resolve it and see if it is a
            // template or a type.
            //
            try
            {
              tree t  (resolve_type (p, c.scope ()));
              int tc (TREE_CODE (t));

              if (tc == TYPE_DECL)
                ptr = p;
              else if (tc == TEMPLATE_DECL && DECL_CLASS_TEMPLATE_P (t))
                ptr = p + "< " + name + " >";
              else
              {
                cerr << c.file () << ":" << c.line () << ":" << c.column ()
                     << ": error: name '" << p << "' specified with "
                     << "'#pragma object pointer' does not name a type "
                     << "or a template" << endl;

                throw generation_failed ();
              }
            }
            catch (invalid_name const&)
            {
              cerr << c.file () << ":" << c.line () << ":" << c.column ()
                   << ": error: type name '" << p << "' specified with "
                   << "'#pragma object pointer' is invalid" << endl;

              throw generation_failed ();
            }
            catch (unable_to_resolve const&)
            {
              cerr << c.file () << ":" << c.line () << ":" << c.column ()
                   << ": error: unable to resolve type name '" << p
                   << "' specified with '#pragma object pointer'" << endl;

              throw generation_failed ();
            }
          }
        }
        else
        {
          // Use the default pointer.
          //
          string const& p (options.default_pointer ());

          if (p == "*")
            ptr = name + "*";
          else
            ptr = p + "< " + name + " >";
        }

        c.set ("object-pointer", ptr);
      }
    }

  private:
    struct invalid_name {};
    struct unable_to_resolve {};

    tree
    resolve_type (string const& qn, semantics::scope& ss)
    {
      tree scope (ss.tree_node ());

      for (size_t b (0), e (qn.find (':')), size (qn.size ());;
           e = qn.find (':', b))
      {
        bool last (e == string::npos);
        string n (qn, b, last ? string::npos : e - b);

        if (n.empty ())
        {
          if (b == 0)
            scope = global_namespace;
          else
            throw invalid_name ();
        }
        else
        {
          tree nid (get_identifier (n.c_str ()));
          scope = lookup_qualified_name (scope, nid, last, false);

          // If this is the first component in the name, then also
          // search the outer scopes.
          //
          if (scope == error_mark_node && b == 0 && !ss.global_scope ())
          {
            semantics::scope* s (&ss);
            do
            {
              s = &s->scope_ ();
              scope = lookup_qualified_name (
                s->tree_node (), nid, last, false);
            } while (scope == error_mark_node && !s->global_scope ());
          }

          if (scope == error_mark_node)
            throw unable_to_resolve ();

          if (!last && TREE_CODE (scope) == TYPE_DECL)
            scope = TREE_TYPE (scope);
        }

        if (e == string::npos)
          break;

        if (qn[++e] != ':')
          throw invalid_name ();

        ++e; // Second ':'.

        if (e == size)
          break;

        b = e;
      }

      return scope;
    }

  private:
    data_member member_;
    traversal::names member_names_;
  };
}

void
process_types (context& ctx)
{
  traversal::unit unit;
  traversal::defines unit_defines;
  traversal::namespace_ ns;
  class_ c (ctx);

  unit >> unit_defines >> ns;
  unit_defines >> c;

  traversal::defines ns_defines;

  ns >> ns_defines >> ns;
  ns_defines >> c;

  unit.dispatch (ctx.unit);
}