summaryrefslogtreecommitdiff
path: root/libxsd-frontend/xsd-frontend/semantic-graph/any-attribute.cxx
blob: 19b9df2a512df39c5ad39b036f297e428126caa4 (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
// file      : xsd-frontend/semantic-graph/any-attribute.cxx
// copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

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

#include <xsd-frontend/semantic-graph/any-attribute.hxx>
#include <xsd-frontend/semantic-graph/compositors.hxx>

namespace XSDFrontend
{
  namespace SemanticGraph
  {
    AnyAttribute::
    AnyAttribute (Path const& file,
                  unsigned long line,
                  unsigned long column,
                  String const& namespaces)
        : Node (file, line, column),
          prototype_ (0)
    {
      // Not sure if the separator is just space or any white-space
      // chararcter.
      //

      for (size_t i (0), j (namespaces.find (L' '));;)
      {
        if (j != String::npos)
        {
          namespaces_.push_back (String (namespaces, i, j - i));

          i = j + 1;
          j = namespaces.find (L' ', i);
        }
        else
        {
          // Last element.
          //
          namespaces_.push_back (String (namespaces, i));
          break;
        }
      }
    }

    AnyAttribute::
    AnyAttribute (Path const& file,
                  unsigned long line,
                  unsigned long column,
                  NamespaceIterator begin,
                  NamespaceIterator end)
        : Node (file, line, column),
          prototype_ (0)
    {
      for (; begin != end; ++begin)
        namespaces_.push_back (*begin);
    }

    namespace
    {
      Namespace&
      namespace_ (Nameable& n)
      {
        // The basic idea goes like this: go up Names edges until you
        // reach Namespace. There are, however, anonymous types which
        // need special handling. In the case of an anonymous type we
        // will go up the first Belongs edge (because the first edge
        // is where the type was defined.
        //

        if (n.named_p ())
        {
          Scope& s (n.scope ());
          Namespace* ns (dynamic_cast<Namespace*> (&n));

          return ns ? *ns : namespace_ (s);
        }
        else
        {
          Type& t (dynamic_cast<Type&> (n));
          Belongs& b (*t.classifies_begin ());

          return namespace_ (b.instance ());
        }
      }
    }

    Namespace& AnyAttribute::
    definition_namespace ()
    {
      if (prototype_p ())
        return prototype ().definition_namespace ();

      return namespace_ (scope ());
    }

    namespace
    {
      using compiler::type_info;

      struct AnyAttributeInit
      {
        AnyAttributeInit ()
        {
          type_info ti (typeid (AnyAttribute));
          ti.add_base (typeid (Nameable));
          insert (ti);
        }
      } any_attribute_init_;
    }
  }
}