blob: 7ddc8893100d6ffee1c8d47fcb7c0c6d1f5efa13 [file] [log] [blame]
Timur Iskhakov505316c2017-08-05 03:38:59 +00001/*
2 * Copyright (C) 2017 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 REFERENCE_H_
18
19#define REFERENCE_H_
20
21#include <android-base/logging.h>
22#include <hidl-util/FQName.h>
23
Steven Moreland49bad8d2018-05-17 15:45:26 -070024#include "DocComment.h"
Timur Iskhakov505316c2017-08-05 03:38:59 +000025#include "Location.h"
26
27namespace android {
28
29/**
30 * Reference placeholder
31 */
32template <class T>
33struct Reference {
34 Reference() = default;
Timur Iskhakov774ae7f2017-08-13 22:57:55 -070035 virtual ~Reference() {}
Timur Iskhakov505316c2017-08-05 03:38:59 +000036
37 Reference(const FQName& fqName, const Location& location)
38 : mResolved(nullptr), mFqName(fqName), mLocation(location) {}
39
40 Reference(T* type, const Location& location) : mResolved(type), mLocation(location) {
41 CHECK(type != nullptr);
42 }
43
Timur Iskhakov33431e62017-08-21 17:31:23 -070044 template <class OtherT>
45 Reference(const Reference<OtherT>& ref)
Timur Iskhakov505316c2017-08-05 03:38:59 +000046 : mResolved(ref.mResolved), mFqName(ref.mFqName), mLocation(ref.mLocation) {}
47
Timur Iskhakova48a4d92017-09-09 20:34:53 -070048 template <class OtherT>
49 Reference(const Reference<OtherT>& ref, const Location& location)
50 : mResolved(ref.mResolved), mFqName(ref.mFqName), mLocation(location) {}
51
Timur Iskhakov505316c2017-08-05 03:38:59 +000052 /* Returns true iff referred type is resolved
53 Referred type's field might be not resolved */
54 bool isResolved() const { return mResolved != nullptr; }
55
Timur Iskhakov24e605b2017-08-30 14:02:55 -070056 T* operator->() { return get(); }
57 const T* operator->() const { return get(); }
Timur Iskhakov505316c2017-08-05 03:38:59 +000058
Timur Iskhakovdbaed332017-08-31 16:33:41 -070059 /* Returns referenced object.
Andreas Huber4ba5c972017-11-29 11:06:25 -080060 If a type is referenced, all typedefs are unwrapped. */
Timur Iskhakov24e605b2017-08-30 14:02:55 -070061 T* get() {
62 CHECK(mResolved != nullptr);
Timur Iskhakovdbaed332017-08-31 16:33:41 -070063 return mResolved->resolve();
64 }
65 const T* get() const {
66 CHECK(mResolved != nullptr);
67 return mResolved->resolve();
68 }
69
70 /* Returns exact referenced object.
71 If a type is referenced, typedefs are not unwraped. */
72 T* shallowGet() {
73 CHECK(mResolved != nullptr);
Timur Iskhakov24e605b2017-08-30 14:02:55 -070074 return mResolved;
75 }
Timur Iskhakovdbaed332017-08-31 16:33:41 -070076 const T* shallowGet() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +000077 CHECK(mResolved != nullptr);
78 return mResolved;
79 }
80
Timur Iskhakov505316c2017-08-05 03:38:59 +000081 void set(T* resolved) {
82 CHECK(!isResolved());
83 CHECK(resolved != nullptr);
84 mResolved = resolved;
85 }
86
87 /* Returns true iff this is reference to null:
88 not resolved and has not name for lookup */
89 bool isEmptyReference() const { return !isResolved() && !hasLookupFqName(); }
90
91 const FQName& getLookupFqName() const {
92 CHECK(hasLookupFqName());
93 return mFqName;
94 }
95
96 bool hasLocation() const { return mLocation.isValid(); }
97
Timur Iskhakovcec46c42017-08-09 00:22:02 -070098 const Location& location() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +000099 CHECK(hasLocation());
100 return mLocation;
101 }
102
103 private:
104 /* Referred type */
105 T* mResolved = nullptr;
106 /* Reference name for lookup */
107 FQName mFqName;
Timur Iskhakova48a4d92017-09-09 20:34:53 -0700108 /* Reference location is mainly used for printing errors
109 and handling forward reference restrictions */
Timur Iskhakov505316c2017-08-05 03:38:59 +0000110 Location mLocation;
111
112 bool hasLookupFqName() const {
113 // Valid only while not resolved to prevent confusion when
114 // ref.hasLookupFqName() is false while ref,get()->fqName is valid.
115 CHECK(!isResolved());
116 return mFqName.isValid();
117 }
118
119 template <class OtherT>
120 friend struct Reference;
121};
122
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700123template <class T>
Steven Moreland49bad8d2018-05-17 15:45:26 -0700124struct NamedReference : public Reference<T>, DocCommentable {
Timur Iskhakova48a4d92017-09-09 20:34:53 -0700125 NamedReference(const std::string& name, const Reference<T>& reference, const Location& location)
126 : Reference<T>(reference, location), mName(name) {}
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700127
128 const std::string& name() const { return mName; }
Timur Iskhakov774ae7f2017-08-13 22:57:55 -0700129
130 // TODO(b/64715470) Legacy
131 const T& type() const { return *Reference<T>::get(); }
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700132
133 private:
134 const std::string mName;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700135};
136
Timur Iskhakov505316c2017-08-05 03:38:59 +0000137} // namespace android
138
139#endif // REFERENCE_H_