Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #ifndef THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_TEMPLATES_H_ |
| 6 | #define THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_TEMPLATES_H_ |
| 7 | |
Devin Jeanpierre | 5641325 | 2022-06-02 18:48:24 -0700 | [diff] [blame] | 8 | #pragma clang lifetime_elision |
| 9 | |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 10 | struct DifferentScope final {}; |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 11 | |
| 12 | namespace test_namespace_bindings { |
| 13 | |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 14 | template <typename T> |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 15 | class MyTemplate final { |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 16 | public: |
| 17 | static MyTemplate Create(T value) { |
| 18 | MyTemplate result; |
| 19 | result.value_ = value; |
| 20 | return result; |
| 21 | } |
| 22 | |
| 23 | const T& value() const { return value_; } |
| 24 | |
| 25 | private: |
| 26 | T value_; |
| 27 | }; |
| 28 | |
| 29 | using MyTypeAlias = MyTemplate<int>; |
| 30 | using OtherTypeAliasInSameTarget = MyTemplate<int>; |
| 31 | |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 32 | struct TemplateParam final {}; |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 33 | using TemplateWithStructTemplateParam = MyTemplate<TemplateParam>; |
| 34 | using ParamFromDifferentScope = MyTemplate<DifferentScope>; |
| 35 | |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 36 | template <typename T1, typename T2> |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 37 | struct TemplateWithTwoParams final { |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 38 | T1 value1; |
| 39 | T2 value2; |
| 40 | }; |
| 41 | |
| 42 | using AliasToTemplateWithTwoParams = TemplateWithTwoParams<int, float>; |
| 43 | |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 44 | using AliasToTemplateOfATemplate = |
| 45 | TemplateWithTwoParams<TemplateWithTwoParams<int, int>, int>; |
| 46 | |
Rosica Dejanovska | d9d2f39 | 2022-08-24 05:31:13 -0700 | [diff] [blame] | 47 | template <typename T> |
| 48 | struct MyStruct { |
| 49 | T t; |
| 50 | }; |
| 51 | |
Marcel Hlopko | eecc285 | 2022-09-08 06:30:51 -0700 | [diff] [blame] | 52 | // Explicit class template specialization with definition should not be imported |
| 53 | // unless also instantiated. |
Marcel Hlopko | eecc285 | 2022-09-08 06:30:51 -0700 | [diff] [blame] | 54 | template <> |
| 55 | struct MyStruct<bool> {}; |
| 56 | |
| 57 | // Explicit class template specialization with definition should be imported |
| 58 | // even when not instantiated if there is a type alias for it. |
Rosica Dejanovska | d9d2f39 | 2022-08-24 05:31:13 -0700 | [diff] [blame] | 59 | template <> |
| 60 | struct MyStruct<char> {}; |
Marcel Hlopko | eecc285 | 2022-09-08 06:30:51 -0700 | [diff] [blame] | 61 | using MyCharStruct = MyStruct<char>; |
| 62 | |
| 63 | // Forward declared explicit class template specialization should be imported |
| 64 | // so the forward declaration code is generated (`forward_declare!`). |
| 65 | template <> |
| 66 | struct MyStruct<int>; |
| 67 | |
| 68 | // Explicit class template instantiation definition is imported similarly to |
| 69 | // how implicit typedeffed instantiations are. |
| 70 | template class MyStruct<float>; |
| 71 | |
| 72 | // Explicit class template instantiation declaration is not handled (yet?) |
| 73 | // TODO(b/245467707): Consider handling these as a build speed/ergonomic |
| 74 | // optimization. |
| 75 | extern template class MyStruct<double>; |
Rosica Dejanovska | d9d2f39 | 2022-08-24 05:31:13 -0700 | [diff] [blame] | 76 | |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 77 | } // namespace test_namespace_bindings |
| 78 | |
| 79 | template <typename T> |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 80 | struct MyTopLevelTemplate final { |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 81 | T value; |
| 82 | }; |
| 83 | |
| 84 | using TopLevelTemplateWithNonTopLevelParam = |
| 85 | MyTopLevelTemplate<test_namespace_bindings::TemplateParam>; |
| 86 | |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 87 | template <> |
| 88 | struct MyTopLevelTemplate<int>; |
| 89 | |
| 90 | void processForwardDeclaredSpecialization(MyTopLevelTemplate<int>* i); |
| 91 | |
Lukasz Anforowicz | 6d1aadf | 2022-08-31 09:04:35 -0700 | [diff] [blame] | 92 | namespace template_template_params { |
| 93 | |
| 94 | template <typename TPolicyType> |
| 95 | struct Policy { |
| 96 | static constexpr TPolicyType policy = TPolicyType(); |
| 97 | }; |
| 98 | |
| 99 | template <> |
| 100 | struct Policy<int> { |
| 101 | static constexpr int policy = 42; |
| 102 | }; |
| 103 | |
| 104 | template <template <class> class TPolicy> |
| 105 | class MyTemplate { |
| 106 | public: |
| 107 | static int GetPolicy() { return TPolicy<int>::policy; } |
| 108 | }; |
| 109 | |
| 110 | using MyTypeAlias = MyTemplate<Policy>; |
| 111 | |
| 112 | } // namespace template_template_params |
| 113 | |
Lukasz Anforowicz | f6c8b4d | 2022-09-01 06:58:17 -0700 | [diff] [blame] | 114 | // This namespace is a regression test for b/244227110 that is based on |
Lukasz Anforowicz | 42ab93b | 2022-08-31 11:17:59 -0700 | [diff] [blame] | 115 | // `<iosfwd>`: |
| 116 | // - `ForwardDeclaredTemplate` corresponds roughly to the `basic_ios` class |
| 117 | // template. |
| 118 | // - `TypeAliasToForwardDeclaredTemplate` corresponds toughtly to the |
| 119 | // `typedef basic_ios<char> ios` type alias. |
Lukasz Anforowicz | f6c8b4d | 2022-09-01 06:58:17 -0700 | [diff] [blame] | 120 | namespace forward_declared_template { |
Lukasz Anforowicz | 42ab93b | 2022-08-31 11:17:59 -0700 | [diff] [blame] | 121 | |
| 122 | template <typename T> |
| 123 | class ForwardDeclaredTemplate; |
| 124 | |
| 125 | using TypeAliasToForwardDeclaredTemplate = ForwardDeclaredTemplate<int>; |
| 126 | |
| 127 | } // namespace forward_declared_template |
| 128 | |
Devin Jeanpierre | 4223859 | 2022-10-04 20:27:44 -0700 | [diff] [blame] | 129 | namespace private_classes { |
| 130 | |
| 131 | class HasPrivateType { |
| 132 | private: |
| 133 | struct PrivateType { |
| 134 | using Foo = test_namespace_bindings::MyTemplate<PrivateType>; |
| 135 | Foo* get(); |
| 136 | }; |
| 137 | |
| 138 | protected: |
| 139 | HasPrivateType(test_namespace_bindings::MyTemplate<PrivateType> x) {} |
| 140 | }; |
| 141 | } // namespace private_classes |
| 142 | |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 143 | #endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_TEMPLATES_H_ |