blob: 9db64ab60e4d80e48c917831ed825cfc3089aab3 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_LINKER_H
18#define AAPT_LINKER_H
19
20#include "Resolver.h"
21#include "ResourceTable.h"
22#include "ResourceValues.h"
23#include "Source.h"
24#include "StringPiece.h"
25
26#include <androidfw/AssetManager.h>
27#include <map>
28#include <memory>
29#include <ostream>
30#include <set>
31#include <vector>
32
33namespace aapt {
34
35/**
36 * The Linker has two jobs. It follows resource references
37 * and verifies that their targert exists and that their
38 * types are compatible. The Linker will also assign resource
39 * IDs and fill in all the dependent references with the newly
40 * assigned resource IDs.
41 *
42 * To do this, the Linker builds a graph of references. This
43 * can be useful to do other analysis, like building a
44 * dependency graph of source files. The hope is to be able to
45 * add functionality that operates on the graph without
46 * overcomplicating the Linker.
47 *
48 * TODO(adamlesinski): Build the graph first then run the separate
49 * steps over the graph.
50 */
51class Linker : ValueVisitor {
52public:
53 /**
54 * Create a Linker for the given resource table with the sources available in
Adam Lesinski24aad162015-04-24 19:19:30 -070055 * IResolver. IResolver should contain the ResourceTable as a source too.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 */
Adam Lesinski24aad162015-04-24 19:19:30 -070057 Linker(std::shared_ptr<ResourceTable> table, std::shared_ptr<IResolver> resolver);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
59 Linker(const Linker&) = delete;
60
61 /**
62 * Entry point to the linker. Assigns resource IDs, follows references,
63 * and validates types. Returns true if all references to defined values
64 * are type-compatible. Missing resource references are recorded but do
65 * not cause this method to fail.
66 */
67 bool linkAndValidate();
68
69 /**
70 * Returns any references to resources that were not defined in any of the
71 * sources.
72 */
73 using ResourceNameToSourceMap = std::map<ResourceName, std::vector<SourceLine>>;
74 const ResourceNameToSourceMap& getUnresolvedReferences() const;
75
76private:
77 struct Args : public ValueVisitorArgs {
78 Args(const ResourceNameRef& r, const SourceLine& s);
79
80 const ResourceNameRef& referrer;
81 const SourceLine& source;
82 };
83
84 //
85 // Overrides of ValueVisitor
86 //
87 void visit(Reference& reference, ValueVisitorArgs& args) override;
88 void visit(Attribute& attribute, ValueVisitorArgs& args) override;
89 void visit(Styleable& styleable, ValueVisitorArgs& args) override;
90 void visit(Style& style, ValueVisitorArgs& args) override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091 void visit(Array& array, ValueVisitorArgs& args) override;
92 void visit(Plural& plural, ValueVisitorArgs& args) override;
93
94 void processAttributeValue(const ResourceNameRef& name, const SourceLine& source,
95 const Attribute& attr, std::unique_ptr<Item>& value);
96
97 void addUnresolvedSymbol(const ResourceNameRef& name, const SourceLine& source);
98
99 /**
100 * Node of the resource table graph.
101 */
102 struct Node {
103 // We use ResourceNameRef and StringPiece, which are safe so long as the ResourceTable
104 // that defines the data isn't modified.
105 ResourceNameRef name;
106 StringPiece source;
107 size_t line;
108
109 // The reference object that points to name.
110 Reference* reference;
111
112 bool operator<(const Node& rhs) const;
113 bool operator==(const Node& rhs) const;
114 bool operator!=(const Node& rhs) const;
115 };
116 friend ::std::ostream& operator<<(::std::ostream&, const Node&);
117
118 std::shared_ptr<ResourceTable> mTable;
Adam Lesinski24aad162015-04-24 19:19:30 -0700119 std::shared_ptr<IResolver> mResolver;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120 std::map<ResourceNameRef, std::vector<Node>> mGraph;
121 std::map<ResourceName, std::vector<SourceLine>> mUnresolvedSymbols;
122 bool mError;
123};
124
125} // namespace aapt
126
127#endif // AAPT_LINKER_H