blob: 1256709edbf4e516af40ddfeff21a5f5493e1246 [file] [log] [blame]
Adam Lesinski467f1712015-11-16 17:35:44 -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_REFERENCELINKER_H
18#define AAPT_LINKER_REFERENCELINKER_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include "android-base/macros.h"
21
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "Resource.h"
23#include "ResourceValues.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080024#include "link/Linkers.h"
25#include "process/IResourceTableConsumer.h"
26#include "process/SymbolTable.h"
27#include "xml/XmlDom.h"
28
Adam Lesinski467f1712015-11-16 17:35:44 -080029namespace aapt {
30
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070031// Resolves all references to resources in the ResourceTable and assigns them IDs.
32// The ResourceTable must already have IDs assigned to each resource.
33// Once the ResourceTable is processed by this linker, it is ready to be flattened.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034class ReferenceLinker : public IResourceTableConsumer {
35 public:
36 ReferenceLinker() = default;
37
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070038 // Performs name mangling and looks up the resource in the symbol table. Uses the callsite's
Chris Warrington58e2fbf2018-07-23 14:12:20 +000039 // package if the reference has no package name defined (implicit).
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070040 // Returns nullptr if the symbol was not found.
41 static const SymbolTable::Symbol* ResolveSymbol(const Reference& reference,
Udam Sainib228df32019-06-18 16:50:34 -070042 const CallSite& callsite,
43 IAaptContext* context,
44 SymbolTable* symbols);
Adam Lesinski467f1712015-11-16 17:35:44 -080045
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070046 // Performs name mangling and looks up the resource in the symbol table. If the symbol is not
47 // visible by the reference at the callsite, nullptr is returned.
48 // `out_error` holds the error message.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080049 static const SymbolTable::Symbol* ResolveSymbolCheckVisibility(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -080050 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -070051 IAaptContext* context,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080052 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080053 std::string* out_error);
Adam Lesinski467f1712015-11-16 17:35:44 -080054
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070055 // Same as ResolveSymbolCheckVisibility(), but also makes sure the symbol is an attribute.
56 // That is, the return value will have a non-null value for ISymbolTable::Symbol::attribute.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080057 static const SymbolTable::Symbol* ResolveAttributeCheckVisibility(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -080058 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -070059 IAaptContext* context,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080060 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080061 std::string* out_error);
Adam Lesinski467f1712015-11-16 17:35:44 -080062
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070063 // Resolves the attribute reference and returns an xml::AaptAttribute if successful.
64 // If resolution fails, outError holds the error message.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080065 static Maybe<xml::AaptAttribute> CompileXmlAttribute(const Reference& reference,
Adam Lesinskif34b6f42017-03-03 16:33:26 -080066 const CallSite& callsite,
Udam Sainib228df32019-06-18 16:50:34 -070067 IAaptContext* context,
Chris Warrington58e2fbf2018-07-23 14:12:20 +000068 SymbolTable* symbols,
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080069 std::string* out_error);
Adam Lesinski467f1712015-11-16 17:35:44 -080070
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070071 // Writes the resource name to the DiagMessage, using the
72 // "orig_name (aka <transformed_name>)" syntax.
73 static void WriteResourceName(const Reference& orig, const CallSite& callsite,
74 const xml::IPackageDeclStack* decls, DiagMessage* out_msg);
Adam Lesinski28cacf02015-11-23 14:22:47 -080075
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070076 // Same as WriteResourceName but omits the 'attr' part.
77 static void WriteAttributeName(const Reference& ref, const CallSite& callsite,
78 const xml::IPackageDeclStack* decls, DiagMessage* out_msg);
79
80 // Transforms the package name of the reference to the fully qualified package name using
81 // the xml::IPackageDeclStack, then mangles and looks up the symbol. If the symbol is visible
82 // to the reference at the callsite, the reference is updated with an ID.
83 // Returns false on failure, and an error message is logged to the IDiagnostics in the context.
Adam Lesinskif34b6f42017-03-03 16:33:26 -080084 static bool LinkReference(const CallSite& callsite, Reference* reference, IAaptContext* context,
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070085 SymbolTable* symbols, const xml::IPackageDeclStack* decls);
Adam Lesinski467f1712015-11-16 17:35:44 -080086
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070087 // Links all references in the ResourceTable.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 bool Consume(IAaptContext* context, ResourceTable* table) override;
89
90 private:
91 DISALLOW_COPY_AND_ASSIGN(ReferenceLinker);
Adam Lesinski467f1712015-11-16 17:35:44 -080092};
93
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094} // namespace aapt
Adam Lesinski467f1712015-11-16 17:35:44 -080095
96#endif /* AAPT_LINKER_REFERENCELINKER_H */