aboutsummaryrefslogtreecommitdiff
path: root/build/scope.cxx
blob: 7165663d51febccba59c54d1a2ba566da040a645 (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
// file      : build/scope.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <build/scope>

using namespace std;

namespace build
{
  scope_map scopes;
  scope* root_scope;

  scope& scope_map::
  operator[] (const path& k)
  {
    auto er (emplace (k, scope ()));
    scope& s (er.first->second);

    if (er.second)
    {
      scope* p (nullptr);

      // Update scopes of which we are a new parent.
      //
      for (auto r (find_prefix (k)); r.first != r.second; ++r.first)
      {
        scope& c (r.first->second);

        // The first scope of which we are a parent is the least
        // (shortest) one which means there is no other scope
        // between it and our parent.
        //
        if (p == nullptr)
          p = &c;
        else if (p != &c) // A scope with an intermediate parent.
          continue;

        c.parent (s);
      }

      // We couldn't get the parent from one of its old children
      // so we have to find it ourselves (unless this is is the
      // root scope).
      //
      if (p == nullptr && size () != 1)
        p = &find (k);

      s.init (er.first, p);
    }

    return s;
  }

  // Find the most qualified scope that encompasses this path.
  //
  scope& scope_map::
  find (const path& k)
  {
    // Normally we would have a scope for the full path so try
    // that before making any copies.
    //
    auto i (base::find (k));

    if (i != end ())
      return i->second;

    for (path d (k.directory ());; d = d.directory ())
    {
      auto i (base::find (k));

      if (i != end ())
        return i->second;

      assert (d.empty ()); // We should have the root scope.
    }
  }
}