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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
// Copyright (c) 2013 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.
#include "tools/gn/scope.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "tools/gn/parse_tree.h"
#include "tools/gn/template.h"
namespace {
// FLags set in the mode_flags_ of a scope. If a bit is set, it applies
// recursively to all dependent scopes.
const unsigned kProcessingBuildConfigFlag = 1;
const unsigned kProcessingImportFlag = 2;
} // namespace
Scope::Scope(const Settings* settings)
: const_containing_(NULL),
mutable_containing_(NULL),
settings_(settings),
mode_flags_(0) {
}
Scope::Scope(Scope* parent)
: const_containing_(NULL),
mutable_containing_(parent),
settings_(parent->settings()),
mode_flags_(0) {
}
Scope::Scope(const Scope* parent)
: const_containing_(parent),
mutable_containing_(NULL),
settings_(parent->settings()),
mode_flags_(0) {
}
Scope::~Scope() {
STLDeleteContainerPairSecondPointers(target_defaults_.begin(),
target_defaults_.end());
STLDeleteContainerPairSecondPointers(templates_.begin(), templates_.end());
}
const Value* Scope::GetValue(const base::StringPiece& ident,
bool counts_as_used) {
// First check for programatically-provided values.
for (ProviderSet::const_iterator i = programmatic_providers_.begin();
i != programmatic_providers_.end(); ++i) {
const Value* v = (*i)->GetProgrammaticValue(ident);
if (v)
return v;
}
RecordMap::iterator found = values_.find(ident);
if (found != values_.end()) {
if (counts_as_used)
found->second.used = true;
return &found->second.value;
}
// Search in the parent scope.
if (const_containing_)
return const_containing_->GetValue(ident);
if (mutable_containing_)
return mutable_containing_->GetValue(ident, counts_as_used);
return NULL;
}
Value* Scope::GetMutableValue(const base::StringPiece& ident,
bool counts_as_used) {
// Don't do programatic values, which are not mutable.
RecordMap::iterator found = values_.find(ident);
if (found != values_.end()) {
if (counts_as_used)
found->second.used = true;
return &found->second.value;
}
// Search in the parent mutable scope, but not const one.
if (mutable_containing_)
return mutable_containing_->GetMutableValue(ident, counts_as_used);
return NULL;
}
Value* Scope::GetValueForcedToCurrentScope(const base::StringPiece& ident,
const ParseNode* set_node) {
RecordMap::iterator found = values_.find(ident);
if (found != values_.end())
return &found->second.value; // Already have in the current scope.
// Search in the parent scope.
if (containing()) {
const Value* in_containing = containing()->GetValue(ident);
if (in_containing) {
// Promote to current scope.
return SetValue(ident, *in_containing, set_node);
}
}
return NULL;
}
const Value* Scope::GetValue(const base::StringPiece& ident) const {
RecordMap::const_iterator found = values_.find(ident);
if (found != values_.end())
return &found->second.value;
if (containing())
return containing()->GetValue(ident);
return NULL;
}
Value* Scope::SetValue(const base::StringPiece& ident,
const Value& v,
const ParseNode* set_node) {
Record& r = values_[ident]; // Clears any existing value.
r.value = v;
r.value.set_origin(set_node);
return &r.value;
}
bool Scope::AddTemplate(const std::string& name, scoped_ptr<Template> templ) {
if (GetTemplate(name))
return false;
templates_[name] = templ.release();
return true;
}
const Template* Scope::GetTemplate(const std::string& name) const {
TemplateMap::const_iterator found = templates_.find(name);
if (found != templates_.end())
return found->second;
if (containing())
return containing()->GetTemplate(name);
return NULL;
}
void Scope::MarkUsed(const base::StringPiece& ident) {
RecordMap::iterator found = values_.find(ident);
if (found == values_.end()) {
NOTREACHED();
return;
}
found->second.used = true;
}
void Scope::MarkUnused(const base::StringPiece& ident) {
RecordMap::iterator found = values_.find(ident);
if (found == values_.end()) {
NOTREACHED();
return;
}
found->second.used = false;
}
bool Scope::IsSetButUnused(const base::StringPiece& ident) const {
RecordMap::const_iterator found = values_.find(ident);
if (found != values_.end()) {
if (!found->second.used) {
return true;
}
}
return false;
}
bool Scope::CheckForUnusedVars(Err* err) const {
for (RecordMap::const_iterator i = values_.begin();
i != values_.end(); ++i) {
if (!i->second.used) {
std::string help = "You set the variable \"" + i->first.as_string() +
"\" here and it was unused before it went\nout of scope.";
const BinaryOpNode* binary = i->second.value.origin()->AsBinaryOp();
if (binary && binary->op().type() == Token::EQUAL) {
// Make a nicer error message for normal var sets.
*err = Err(binary->left()->GetRange(), "Assignment had no effect.",
help);
} else {
// This will happen for internally-generated variables.
*err = Err(i->second.value.origin(), "Assignment had no effect.", help);
}
return false;
}
}
return true;
}
void Scope::GetCurrentScopeValues(KeyValueMap* output) const {
for (RecordMap::const_iterator i = values_.begin(); i != values_.end(); ++i)
(*output)[i->first] = i->second.value;
}
bool Scope::NonRecursiveMergeTo(Scope* dest,
bool clobber_existing,
const ParseNode* node_for_err,
const char* desc_for_err,
Err* err) const {
// Values.
for (RecordMap::const_iterator i = values_.begin(); i != values_.end(); ++i) {
const Value& new_value = i->second.value;
if (!clobber_existing) {
const Value* existing_value = dest->GetValue(i->first);
if (existing_value && new_value != *existing_value) {
// Value present in both the source and the dest.
std::string desc_string(desc_for_err);
*err = Err(node_for_err, "Value collision.",
"This " + desc_string + " contains \"" + i->first.as_string() +
"\"");
err->AppendSubErr(Err(i->second.value, "defined here.",
"Which would clobber the one in your current scope"));
err->AppendSubErr(Err(*existing_value, "defined here.",
"Executing " + desc_string + " should not conflict with anything "
"in the current\nscope unless the values are identical."));
return false;
}
}
dest->values_[i->first] = i->second;
}
// Target defaults are owning pointers.
for (NamedScopeMap::const_iterator i = target_defaults_.begin();
i != target_defaults_.end(); ++i) {
if (!clobber_existing) {
if (dest->GetTargetDefaults(i->first)) {
// TODO(brettw) it would be nice to know the origin of a
// set_target_defaults so we can give locations for the colliding target
// defaults.
std::string desc_string(desc_for_err);
*err = Err(node_for_err, "Target defaults collision.",
"This " + desc_string + " contains target defaults for\n"
"\"" + i->first + "\" which would clobber one for the\n"
"same target type in your current scope. It's unfortunate that I'm "
"too stupid\nto tell you the location of where the target defaults "
"were set. Usually\nthis happens in the BUILDCONFIG.gn file.");
return false;
}
}
// Be careful to delete any pointer we're about to clobber.
Scope** dest_scope = &dest->target_defaults_[i->first];
if (*dest_scope)
delete *dest_scope;
*dest_scope = new Scope(settings_);
i->second->NonRecursiveMergeTo(*dest_scope, clobber_existing, node_for_err,
"<SHOULDN'T HAPPEN>", err);
}
// Sources assignment filter.
if (sources_assignment_filter_) {
if (!clobber_existing) {
if (dest->GetSourcesAssignmentFilter()) {
// Sources assignment filter present in both the source and the dest.
std::string desc_string(desc_for_err);
*err = Err(node_for_err, "Assignment filter collision.",
"The " + desc_string + " contains a sources_assignment_filter "
"which\nwould clobber the one in your current scope.");
return false;
}
}
dest->sources_assignment_filter_.reset(
new PatternList(*sources_assignment_filter_));
}
// Templates.
for (TemplateMap::const_iterator i = templates_.begin();
i != templates_.end(); ++i) {
if (!clobber_existing) {
const Template* existing_template = dest->GetTemplate(i->first);
if (existing_template) {
// Rule present in both the source and the dest.
std::string desc_string(desc_for_err);
*err = Err(node_for_err, "Template collision.",
"This " + desc_string + " contains a template \"" +
i->first + "\"");
err->AppendSubErr(Err(i->second->GetDefinitionRange(), "defined here.",
"Which would clobber the one in your current scope"));
err->AppendSubErr(Err(existing_template->GetDefinitionRange(),
"defined here.",
"Executing " + desc_string + " should not conflict with anything "
"in the current\nscope."));
return false;
}
}
// Be careful to delete any pointer we're about to clobber.
const Template** dest_template = &dest->templates_[i->first];
if (*dest_template)
delete *dest_template;
*dest_template = i->second;
}
return true;
}
scoped_ptr<Scope> Scope::MakeClosure() const {
scoped_ptr<Scope> result;
if (const_containing_) {
// We reached the top of the mutable scope stack. The result scope just
// references the const scope (which will never change).
result.reset(new Scope(const_containing_));
} else if (mutable_containing_) {
// There are more nested mutable scopes. Recursively go up the stack to
// get the closure.
result = mutable_containing_->MakeClosure();
} else {
// This is a standalone scope, just copy it.
result.reset(new Scope(settings_));
}
// Add in our variables and we're done.
Err err;
NonRecursiveMergeTo(result.get(), true, NULL, "<SHOULDN'T HAPPEN>", &err);
DCHECK(!err.has_error());
return result.Pass();
}
Scope* Scope::MakeTargetDefaults(const std::string& target_type) {
if (GetTargetDefaults(target_type))
return NULL;
Scope** dest = &target_defaults_[target_type];
if (*dest) {
NOTREACHED(); // Already set.
return *dest;
}
*dest = new Scope(settings_);
return *dest;
}
const Scope* Scope::GetTargetDefaults(const std::string& target_type) const {
NamedScopeMap::const_iterator found = target_defaults_.find(target_type);
if (found != target_defaults_.end())
return found->second;
if (containing())
return containing()->GetTargetDefaults(target_type);
return NULL;
}
const PatternList* Scope::GetSourcesAssignmentFilter() const {
if (sources_assignment_filter_)
return sources_assignment_filter_.get();
if (containing())
return containing()->GetSourcesAssignmentFilter();
return NULL;
}
void Scope::SetProcessingBuildConfig() {
DCHECK((mode_flags_ & kProcessingBuildConfigFlag) == 0);
mode_flags_ |= kProcessingBuildConfigFlag;
}
void Scope::ClearProcessingBuildConfig() {
DCHECK(mode_flags_ & kProcessingBuildConfigFlag);
mode_flags_ &= ~(kProcessingBuildConfigFlag);
}
bool Scope::IsProcessingBuildConfig() const {
if (mode_flags_ & kProcessingBuildConfigFlag)
return true;
if (containing())
return containing()->IsProcessingBuildConfig();
return false;
}
void Scope::SetProcessingImport() {
DCHECK((mode_flags_ & kProcessingImportFlag) == 0);
mode_flags_ |= kProcessingImportFlag;
}
void Scope::ClearProcessingImport() {
DCHECK(mode_flags_ & kProcessingImportFlag);
mode_flags_ &= ~(kProcessingImportFlag);
}
bool Scope::IsProcessingImport() const {
if (mode_flags_ & kProcessingImportFlag)
return true;
if (containing())
return containing()->IsProcessingImport();
return false;
}
const SourceDir& Scope::GetSourceDir() const {
if (!source_dir_.is_null())
return source_dir_;
if (containing())
return containing()->GetSourceDir();
return source_dir_;
}
void Scope::SetProperty(const void* key, void* value) {
if (!value) {
DCHECK(properties_.find(key) != properties_.end());
properties_.erase(key);
} else {
properties_[key] = value;
}
}
void* Scope::GetProperty(const void* key, const Scope** found_on_scope) const {
PropertyMap::const_iterator found = properties_.find(key);
if (found != properties_.end()) {
if (found_on_scope)
*found_on_scope = this;
return found->second;
}
if (containing())
return containing()->GetProperty(key, found_on_scope);
return NULL;
}
void Scope::AddProvider(ProgrammaticProvider* p) {
programmatic_providers_.insert(p);
}
void Scope::RemoveProvider(ProgrammaticProvider* p) {
DCHECK(programmatic_providers_.find(p) != programmatic_providers_.end());
programmatic_providers_.erase(p);
}
|