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