Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 26 | #include "DocComment.h" |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 27 | #include "Location.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | /** |
| 32 | * Reference placeholder |
| 33 | */ |
| 34 | template <class T> |
| 35 | struct Reference { |
| 36 | Reference() = default; |
Timur Iskhakov | 774ae7f | 2017-08-13 22:57:55 -0700 | [diff] [blame] | 37 | virtual ~Reference() {} |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 38 | |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 39 | Reference(const std::string& localName, const FQName& fqName, const Location& location, |
| 40 | bool definedInline = false) |
| 41 | : mResolved(nullptr), |
| 42 | mFqName(fqName), |
| 43 | mLocation(location), |
| 44 | mDefinedInline(definedInline), |
| 45 | mLocalName(localName) {} |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 46 | |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 47 | Reference(const std::string& localName, T* type, const Location& location, |
| 48 | bool definedInline = false) |
| 49 | : mResolved(type), |
| 50 | mLocation(location), |
| 51 | mDefinedInline(definedInline), |
| 52 | mLocalName(localName) { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 53 | CHECK(type != nullptr); |
| 54 | } |
| 55 | |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 56 | template <class OtherT> |
| 57 | Reference(const Reference<OtherT>& ref) |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 58 | : mResolved(ref.mResolved), |
| 59 | mFqName(ref.mFqName), |
| 60 | mLocation(ref.mLocation), |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 61 | mDefinedInline(ref.mDefinedInline), |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 62 | mLocalName(ref.mLocalName) {} |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 63 | |
Timur Iskhakov | a48a4d9 | 2017-09-09 20:34:53 -0700 | [diff] [blame] | 64 | template <class OtherT> |
| 65 | Reference(const Reference<OtherT>& ref, const Location& location) |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 66 | : mResolved(ref.mResolved), |
| 67 | mFqName(ref.mFqName), |
| 68 | mLocation(location), |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 69 | mDefinedInline(ref.mDefinedInline), |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 70 | mLocalName(ref.mLocalName) {} |
Timur Iskhakov | a48a4d9 | 2017-09-09 20:34:53 -0700 | [diff] [blame] | 71 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 72 | /* Returns true iff referred type is resolved |
| 73 | Referred type's field might be not resolved */ |
| 74 | bool isResolved() const { return mResolved != nullptr; } |
| 75 | |
Timur Iskhakov | 24e605b | 2017-08-30 14:02:55 -0700 | [diff] [blame] | 76 | T* operator->() { return get(); } |
| 77 | const T* operator->() const { return get(); } |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 78 | |
Timur Iskhakov | dbaed33 | 2017-08-31 16:33:41 -0700 | [diff] [blame] | 79 | /* Returns referenced object. |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 80 | If a type is referenced, all typedefs are unwrapped. */ |
Timur Iskhakov | 24e605b | 2017-08-30 14:02:55 -0700 | [diff] [blame] | 81 | T* get() { |
| 82 | CHECK(mResolved != nullptr); |
Timur Iskhakov | dbaed33 | 2017-08-31 16:33:41 -0700 | [diff] [blame] | 83 | return mResolved->resolve(); |
| 84 | } |
| 85 | const T* get() const { |
| 86 | CHECK(mResolved != nullptr); |
| 87 | return mResolved->resolve(); |
| 88 | } |
| 89 | |
| 90 | /* Returns exact referenced object. |
| 91 | If a type is referenced, typedefs are not unwraped. */ |
| 92 | T* shallowGet() { |
| 93 | CHECK(mResolved != nullptr); |
Timur Iskhakov | 24e605b | 2017-08-30 14:02:55 -0700 | [diff] [blame] | 94 | return mResolved; |
| 95 | } |
Timur Iskhakov | dbaed33 | 2017-08-31 16:33:41 -0700 | [diff] [blame] | 96 | const T* shallowGet() const { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 97 | CHECK(mResolved != nullptr); |
| 98 | return mResolved; |
| 99 | } |
| 100 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 101 | void set(T* resolved) { |
| 102 | CHECK(!isResolved()); |
| 103 | CHECK(resolved != nullptr); |
| 104 | mResolved = resolved; |
| 105 | } |
| 106 | |
| 107 | /* Returns true iff this is reference to null: |
| 108 | not resolved and has not name for lookup */ |
| 109 | bool isEmptyReference() const { return !isResolved() && !hasLookupFqName(); } |
| 110 | |
| 111 | const FQName& getLookupFqName() const { |
| 112 | CHECK(hasLookupFqName()); |
| 113 | return mFqName; |
| 114 | } |
| 115 | |
| 116 | bool hasLocation() const { return mLocation.isValid(); } |
| 117 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 118 | const Location& location() const { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 119 | CHECK(hasLocation()); |
| 120 | return mLocation; |
| 121 | } |
| 122 | |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 123 | bool definedInline() const { return mDefinedInline; } |
| 124 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 125 | const std::string& localName() const { return mLocalName; } |
| 126 | |
| 127 | private: |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 128 | /* Referred type */ |
| 129 | T* mResolved = nullptr; |
| 130 | /* Reference name for lookup */ |
| 131 | FQName mFqName; |
Timur Iskhakov | a48a4d9 | 2017-09-09 20:34:53 -0700 | [diff] [blame] | 132 | /* Reference location is mainly used for printing errors |
| 133 | and handling forward reference restrictions */ |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 134 | Location mLocation; |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 135 | /* definedInline is true if T is defined where it is referenced */ |
| 136 | bool mDefinedInline; |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 137 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 138 | /* Name used in the .hal file */ |
| 139 | std::string mLocalName; |
| 140 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 141 | bool hasLookupFqName() const { |
| 142 | // Valid only while not resolved to prevent confusion when |
| 143 | // ref.hasLookupFqName() is false while ref,get()->fqName is valid. |
| 144 | CHECK(!isResolved()); |
Steven Moreland | 7773089 | 2018-03-06 14:39:41 -0800 | [diff] [blame] | 145 | return mFqName != FQName(); |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | template <class OtherT> |
| 149 | friend struct Reference; |
| 150 | }; |
| 151 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 152 | template <class T> |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 153 | struct NamedReference : public Reference<T>, DocCommentable { |
Timur Iskhakov | a48a4d9 | 2017-09-09 20:34:53 -0700 | [diff] [blame] | 154 | NamedReference(const std::string& name, const Reference<T>& reference, const Location& location) |
| 155 | : Reference<T>(reference, location), mName(name) {} |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 156 | |
| 157 | const std::string& name() const { return mName; } |
Timur Iskhakov | 774ae7f | 2017-08-13 22:57:55 -0700 | [diff] [blame] | 158 | |
| 159 | // TODO(b/64715470) Legacy |
| 160 | const T& type() const { return *Reference<T>::get(); } |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 161 | |
Neel Mehta | 6de3cf8 | 2019-08-06 16:49:33 -0700 | [diff] [blame] | 162 | private: |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 163 | const std::string mName; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 166 | } // namespace android |
| 167 | |
| 168 | #endif // REFERENCE_H_ |