blob: bf7d1cd70ae091bd740cbea929e7b1ed879baaa4 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#ifndef SCOPE_H_
18
19#define SCOPE_H_
20
21#include "NamedType.h"
22
Steven Morelandd537ab02016-09-12 10:32:01 -070023#include <map>
Timur Iskhakov458ca362017-09-12 23:16:03 -070024#include <unordered_map>
Andreas Huber70a59e12016-08-16 12:57:01 -070025#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070026
27namespace android {
28
Timur Iskhakove9ccfa22017-08-14 15:07:03 -070029struct Annotation;
Timur Iskhakov7296af12017-08-09 21:52:48 +000030struct ConstantExpression;
Andreas Huber881227d2016-08-02 14:20:21 -070031struct Formatter;
32struct Interface;
Yifan Hongf24fa852016-09-23 11:03:15 -070033struct LocalIdentifier;
Andreas Huberc9410c72016-07-28 12:18:40 -070034
35struct Scope : public NamedType {
Timur Iskhakov565b0132017-09-06 18:07:11 -070036 Scope(const char* localName, const FQName& fullName, const Location& location, Scope* parent);
Yifan Hongf24fa852016-09-23 11:03:15 -070037 virtual ~Scope();
Andreas Huberc9410c72016-07-28 12:18:40 -070038
Timur Iskhakov565b0132017-09-06 18:07:11 -070039 void addType(NamedType* type);
40
41 status_t validateUniqueNames() const;
Andreas Huber5a545442016-08-03 10:44:56 -070042
Yifan Hong327cfe12016-10-03 10:29:42 -070043 // lookup a type given an FQName.
44 // Assume fqName.package(), fqName.version(), fqName.valueName() is empty.
Yifan Hongae16eed2016-09-23 13:25:25 -070045 NamedType *lookupType(const FQName &fqName) const;
Andreas Huberc9410c72016-07-28 12:18:40 -070046
Yifan Hongf24fa852016-09-23 11:03:15 -070047 virtual LocalIdentifier *lookupIdentifier(const std::string &name) const;
48
Andreas Huber5345ec22016-07-29 13:33:27 -070049 bool isScope() const override;
Andreas Huber881227d2016-08-02 14:20:21 -070050
51 // Returns the single interface or NULL.
52 Interface *getInterface() const;
53
Steven Morelandb47a2622018-07-11 09:04:25 -070054 bool definesInterfaces() const;
Andreas Huberc9410c72016-07-28 12:18:40 -070055
Timur Iskhakove9ccfa22017-08-14 15:07:03 -070056 const std::vector<Annotation*>& annotations() const;
57
58 void setAnnotations(std::vector<Annotation*>* annotations);
59
Timur Iskhakovb58f4182017-08-29 15:19:24 -070060 std::vector<const Type*> getDefinedTypes() const override;
Timur Iskhakov33431e62017-08-21 17:31:23 -070061
Timur Iskhakovb58f4182017-08-29 15:19:24 -070062 std::vector<const ConstantExpression*> getConstantExpressions() const override;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070063
Timur Iskhakov458ca362017-09-12 23:16:03 -070064 void topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder);
65
Steven Moreland6ec9eb92018-02-16 14:21:49 -080066 void emitTypeDeclarations(Formatter& out) const override;
Steven Moreland8e61c5a2017-11-17 15:55:28 -080067 void emitGlobalTypeDeclarations(Formatter& out) const override;
Steven Moreland6ec9eb92018-02-16 14:21:49 -080068 void emitPackageTypeDeclarations(Formatter& out) const override;
Steven Moreland09c6ebe2018-10-09 10:15:48 -070069 void emitPackageTypeHeaderDefinitions(Formatter& out) const override;
Steven Moreland6ec9eb92018-02-16 14:21:49 -080070 void emitPackageHwDeclarations(Formatter& out) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -070071
Steven Moreland6ec9eb92018-02-16 14:21:49 -080072 void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070073
Steven Moreland6ec9eb92018-02-16 14:21:49 -080074 void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070075
Steven Morelandd537ab02016-09-12 10:32:01 -070076 const std::vector<NamedType *> &getSubTypes() const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070077
Steven Moreland6ec9eb92018-02-16 14:21:49 -080078 void emitVtsTypeDeclarations(Formatter& out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070079
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070080 bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
Andreas Huber70a59e12016-08-16 12:57:01 -070081
Andreas Huber019d21d2016-10-03 12:59:47 -070082 void appendToExportedTypesVector(
83 std::vector<const Type *> *exportedTypes) const override;
84
Timur Iskhakov458ca362017-09-12 23:16:03 -070085 private:
Steven Morelandd537ab02016-09-12 10:32:01 -070086 std::vector<NamedType *> mTypes;
87 std::map<std::string, size_t> mTypeIndexByName;
Timur Iskhakove9ccfa22017-08-14 15:07:03 -070088 std::vector<Annotation*> mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -070089
Timur Iskhakov458ca362017-09-12 23:16:03 -070090 bool mTypeOrderChanged = false;
91
Andreas Huberc9410c72016-07-28 12:18:40 -070092 DISALLOW_COPY_AND_ASSIGN(Scope);
93};
94
Steven Moreland0ecc7b82017-07-19 12:59:23 -070095struct RootScope : public Scope {
Timur Iskhakov565b0132017-09-06 18:07:11 -070096 RootScope(const char* localName, const FQName& fullName, const Location& location,
97 Scope* parent);
Steven Moreland0ecc7b82017-07-19 12:59:23 -070098 virtual ~RootScope();
99
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700100 virtual status_t validate() const override;
101
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700102 std::string typeName() const override;
103};
104
Yifan Hongf24fa852016-09-23 11:03:15 -0700105struct LocalIdentifier {
106 LocalIdentifier();
107 virtual ~LocalIdentifier();
108 virtual bool isEnumValue() const;
Timur Iskhakov7296af12017-08-09 21:52:48 +0000109
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700110 const LocalIdentifier* resolve() const;
111 LocalIdentifier* resolve();
112
Timur Iskhakov7296af12017-08-09 21:52:48 +0000113 virtual ConstantExpression* constExpr() const;
Yifan Hongf24fa852016-09-23 11:03:15 -0700114};
115
Andreas Huberc9410c72016-07-28 12:18:40 -0700116} // namespace android
117
118#endif // SCOPE_H_
119