aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/scope.ixx
blob: 9aecd48609024319d3aeec5055f5b02d5cfb25cc (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
// file      : libbuild2/scope.ixx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

namespace build2
{
  // scope
  //
  inline scope* scope::
  strong_scope ()
  {
    return root_ != nullptr
      ? root_->strong_ != nullptr ? root_->strong_ : root_
      : nullptr;
  }

  inline const scope* scope::
  strong_scope () const
  {
    return root_ != nullptr
      ? root_->strong_ != nullptr ? root_->strong_ : root_
      : nullptr;
  }

  inline scope* scope::
  weak_scope ()
  {
    scope* r (root_);
    if (r != nullptr)
      for (; r->parent_->root_ != nullptr; r = r->parent_->root_) ;
    return r;
  }

  inline const scope* scope::
  weak_scope () const
  {
    const scope* r (root_);
    if (r != nullptr)
      for (; r->parent_->root_ != nullptr; r = r->parent_->root_) ;
    return r;
  }

  inline bool scope::
  sub_root (const scope& r) const
  {
    // Scan the parent root scope chain looking for this scope.
    //
    for (const scope* pr (&r); (pr = pr->parent_->root_) != nullptr; )
      if (pr == this)
        return true;

    return false;
  }

  inline target_key scope::
  find_target_key (name& n, name& o, const location& loc) const
  {
    auto p (find_target_type (n, o, loc));
    return target_key {
      &p.first,
      &n.dir,
      o.dir.empty () ? &empty_dir_path : &o.dir,
      &n.value,
      move (p.second)};
  }

  inline prerequisite_key scope::
  find_prerequisite_key (name& n, name& o, const location& loc) const
  {
    auto p (find_prerequisite_type (n, o, loc));
    return prerequisite_key {
      n.proj,
      {
        &p.first,
        &n.dir,
        o.dir.empty () ? &empty_dir_path : &o.dir,
        &n.value,
        move (p.second)
      },
      this};
  }

  inline dir_path
  src_out (const dir_path& out, const scope& r)
  {
    assert (r.root ());
    return src_out (out, r.out_path (), r.src_path ());
  }

  inline dir_path
  out_src (const dir_path& src, const scope& r)
  {
    assert (r.root ());
    return out_src (src, r.out_path (), r.src_path ());
  }

  inline dir_path
  src_out (const dir_path& o,
           const dir_path& out_root, const dir_path& src_root)
  {
    assert (o.sub (out_root));
    return src_root / o.leaf (out_root);
  }

  inline dir_path
  out_src (const dir_path& s,
           const dir_path& out_root, const dir_path& src_root)
  {
    assert (s.sub (src_root));
    return out_root / s.leaf (src_root);
  }

  inline const project_name&
  project (const scope& rs)
  {
    auto l (rs[rs.ctx.var_project]);
    return l ? cast<project_name> (l) : empty_project_name;
  }

  inline const project_name&
  named_project (const scope& rs)
  {
    for (auto r (&rs), a (rs.strong_scope ());
         ;
         r = r->parent_scope ()->root_scope ())
    {
      const project_name& n (project (*r));
      if (!n.empty ())
        return n;

      if (r == a)
        break;
    }

    return empty_project_name;
  }
}