|
| 1 | +// Copyright (c) 2025 Roberto Raggi <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +#include <cxx/ast.h> |
| 22 | +#include <cxx/ast_rewriter.h> |
| 23 | +#include <cxx/control.h> |
| 24 | +#include <cxx/names.h> |
| 25 | +#include <cxx/scope.h> |
| 26 | +#include <cxx/symbol_instantiation.h> |
| 27 | +#include <cxx/symbols.h> |
| 28 | +#include <cxx/translation_unit.h> |
| 29 | +#include <cxx/type_checker.h> |
| 30 | +#include <cxx/types.h> |
| 31 | +#include <gtest/gtest.h> |
| 32 | + |
| 33 | +#include <format> |
| 34 | +#include <iostream> |
| 35 | +#include <sstream> |
| 36 | + |
| 37 | +#include "test_utils.h" |
| 38 | + |
| 39 | +using namespace cxx; |
| 40 | + |
| 41 | +template <typename Node> |
| 42 | +auto subst(Source& source, Node* ast, std::vector<TemplateArgument> args) { |
| 43 | + auto control = source.control(); |
| 44 | + TypeChecker typeChecker(&source.unit); |
| 45 | + ASTRewriter rewrite{&typeChecker, args}; |
| 46 | + return ast_cast<Node>(rewrite(ast)); |
| 47 | +}; |
| 48 | + |
| 49 | +TEST(Rewriter, TypeAlias) { |
| 50 | + auto source = R"( |
| 51 | + template <typename T> |
| 52 | + using Ptr = const T*; |
| 53 | +
|
| 54 | + template <typename T, typename U> |
| 55 | + using Func = void(T, U); |
| 56 | + )"_cxx; |
| 57 | + |
| 58 | + auto control = source.control(); |
| 59 | + |
| 60 | + auto ptrTypeAlias = |
| 61 | + subst(source, source.getAs<TypeAliasSymbol>("Ptr")->declaration(), |
| 62 | + {control->getIntType()}); |
| 63 | + |
| 64 | + ASSERT_EQ(to_string(ptrTypeAlias->typeId->type), "const int*"); |
| 65 | + |
| 66 | + auto funcTypeAlias = |
| 67 | + subst(source, source.getAs<TypeAliasSymbol>("Func")->declaration(), |
| 68 | + {control->getIntType(), control->getFloatType()}); |
| 69 | + |
| 70 | + ASSERT_EQ(to_string(funcTypeAlias->typeId->type), "void (int, float)"); |
| 71 | +} |
0 commit comments