OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_GN_VISIBILITY_H_ | 5 #ifndef TOOLS_GN_VISIBILITY_H_ |
6 #define TOOLS_GN_VISIBILITY_H_ | 6 #define TOOLS_GN_VISIBILITY_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "tools/gn/label_pattern.h" |
12 #include "tools/gn/source_dir.h" | 13 #include "tools/gn/source_dir.h" |
13 | 14 |
14 class Err; | 15 class Err; |
15 class Item; | 16 class Item; |
16 class Label; | 17 class Label; |
17 class Scope; | 18 class Scope; |
18 class Value; | 19 class Value; |
19 | 20 |
20 class Visibility { | 21 class Visibility { |
21 public: | 22 public: |
22 class VisPattern { | |
23 public: | |
24 enum Type { | |
25 MATCH = 1, // Exact match for a given target. | |
26 DIRECTORY, // Only targets in the file in the given directory. | |
27 RECURSIVE_DIRECTORY // The given directory and any subdir. | |
28 // (also indicates "public" when dir is empty). | |
29 }; | |
30 | |
31 VisPattern(); | |
32 VisPattern(Type type, const SourceDir& dir, const base::StringPiece& name); | |
33 ~VisPattern(); | |
34 | |
35 bool Matches(const Label& label) const; | |
36 | |
37 Type type() const { return type_; } | |
38 const SourceDir& dir() const { return dir_; } | |
39 const std::string& name() const { return name_; } | |
40 | |
41 private: | |
42 Type type_; | |
43 | |
44 // Used when type_ == PRIVATE and PRIVATE_RECURSIVE. This specifies the | |
45 // directory that to which the pattern is private to. | |
46 SourceDir dir_; | |
47 | |
48 // Empty name means match everything. Otherwise the name must match | |
49 // exactly. | |
50 std::string name_; | |
51 }; | |
52 | |
53 // Defaults to private visibility (only the current file). | 23 // Defaults to private visibility (only the current file). |
54 Visibility(); | 24 Visibility(); |
55 ~Visibility(); | 25 ~Visibility(); |
56 | 26 |
57 // Set the visibility to the thing specified by the given value. On failure, | 27 // Set the visibility to the thing specified by the given value. On failure, |
58 // returns false and sets the error. | 28 // returns false and sets the error. |
59 bool Set(const SourceDir& current_dir, const Value& value, Err* err); | 29 bool Set(const SourceDir& current_dir, const Value& value, Err* err); |
60 | 30 |
61 // Sets the visibility to be public. | 31 // Sets the visibility to be public. |
62 void SetPublic(); | 32 void SetPublic(); |
63 | 33 |
64 // Sets the visibility to be private to the given directory. | 34 // Sets the visibility to be private to the given directory. |
65 void SetPrivate(const SourceDir& current_dir); | 35 void SetPrivate(const SourceDir& current_dir); |
66 | 36 |
67 // Returns true if the target with the given label can depend on one with the | 37 // Returns true if the target with the given label can depend on one with the |
68 // current visibility. | 38 // current visibility. |
69 bool CanSeeMe(const Label& label) const; | 39 bool CanSeeMe(const Label& label) const; |
70 | 40 |
71 // Returns a string listing the visibility. |indent| number of spaces will | 41 // Returns a string listing the visibility. |indent| number of spaces will |
72 // be added on the left side of the output. If |include_brackets| is set, the | 42 // be added on the left side of the output. If |include_brackets| is set, the |
73 // result will be wrapped in "[ ]" and the contents further indented. The | 43 // result will be wrapped in "[ ]" and the contents further indented. The |
74 // result will end in a newline. | 44 // result will end in a newline. |
75 std::string Describe(int indent, bool include_brackets) const; | 45 std::string Describe(int indent, bool include_brackets) const; |
76 | 46 |
77 // Converts the given input string to a pattern. This does special stuff | |
78 // to treat the pattern as a label. Sets the error on failure. | |
79 static VisPattern GetPattern(const SourceDir& current_dir, | |
80 const Value& value, | |
81 Err* err); | |
82 | |
83 // Helper function to check visibility between the given two items. If | 47 // Helper function to check visibility between the given two items. If |
84 // to is invisible to from, returns false and sets the error. | 48 // to is invisible to from, returns false and sets the error. |
85 static bool CheckItemVisibility(const Item* from, const Item* to, Err* err); | 49 static bool CheckItemVisibility(const Item* from, const Item* to, Err* err); |
86 | 50 |
87 // Helper function to fill an item's visibility from the "visibility" value | 51 // Helper function to fill an item's visibility from the "visibility" value |
88 // in the current scope. | 52 // in the current scope. |
89 static bool FillItemVisibility(Item* item, Scope* scope, Err* err); | 53 static bool FillItemVisibility(Item* item, Scope* scope, Err* err); |
90 | 54 |
91 private: | 55 private: |
92 std::vector<VisPattern> patterns_; | 56 std::vector<LabelPattern> patterns_; |
93 | 57 |
94 DISALLOW_COPY_AND_ASSIGN(Visibility); | 58 DISALLOW_COPY_AND_ASSIGN(Visibility); |
95 }; | 59 }; |
96 | 60 |
97 #endif // TOOLS_GN_VISIBILITY_H_ | 61 #endif // TOOLS_GN_VISIBILITY_H_ |
OLD | NEW |