summaryrefslogtreecommitdiffstats
path: root/tools/gn/visibility.h
blob: 52ec73ca15915811980d464f7d228c57dc671a6e (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TOOLS_GN_VISIBILITY_H_
#define TOOLS_GN_VISIBILITY_H_

#include <vector>

#include "base/basictypes.h"
#include "base/strings/string_piece.h"
#include "tools/gn/source_dir.h"

class Err;
class Item;
class Label;
class Scope;
class Value;

class Visibility {
 public:
  class VisPattern {
   public:
    enum Type {
      MATCH = 1,  // Exact match for a given target.
      DIRECTORY,  // Only targets in the file in the given directory.
      RECURSIVE_DIRECTORY  // The given directory and any subdir.
                           // (also indicates "public" when dir is empty).
    };

    VisPattern();
    VisPattern(Type type, const SourceDir& dir, const base::StringPiece& name);
    ~VisPattern();

    bool Matches(const Label& label) const;

    Type type() const { return type_; }
    const SourceDir& dir() const { return dir_; }
    const std::string& name() const { return name_; }

   private:
    Type type_;

    // Used when type_ == PRIVATE and PRIVATE_RECURSIVE. This specifies the
    // directory that to which the pattern is private to.
    SourceDir dir_;

    // Empty name means match everything. Otherwise the name must match
    // exactly.
    std::string name_;
  };

  // Defaults to private visibility (only the current file).
  Visibility();
  ~Visibility();

  // Set the visibility to the thing specified by the given value. On failure,
  // returns false and sets the error.
  bool Set(const SourceDir& current_dir, const Value& value, Err* err);

  // Sets the visibility to be public.
  void SetPublic();

  // Sets the visibility to be private to the given directory.
  void SetPrivate(const SourceDir& current_dir);

  // Returns true if the target with the given label can depend on one with the
  // current visibility.
  bool CanSeeMe(const Label& label) const;

  // Returns a string listing the visibility.
  std::string Describe() const;

  // Converts the given input string to a pattern. This does special stuff
  // to treat the pattern as a label. Sets the error on failure.
  static VisPattern GetPattern(const SourceDir& current_dir,
                               const Value& value,
                               Err* err);

  // Helper function to check visibility between the given two items. If
  // to is invisible to from, returns false and sets the error.
  static bool CheckItemVisibility(const Item* from, const Item* to, Err* err);

  // Helper function to fill an item's visibility from the "visibility" value
  // in the current scope.
  static bool FillItemVisibility(Item* item, Scope* scope, Err* err);

 private:
  std::vector<VisPattern> patterns_;

  DISALLOW_COPY_AND_ASSIGN(Visibility);
};

#endif  // TOOLS_GN_VISIBILITY_H_