• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VERIFICATION_TYPE_TYPE_IMAGE_H
17 #define VERIFICATION_TYPE_TYPE_IMAGE_H
18 
19 #include "type_type.h"
20 #include "type_params.h"
21 
22 #include "runtime/include/mem/panda_containers.h"
23 #include "runtime/include/mem/panda_string.h"
24 
25 namespace panda::verifier {
26 class TypeImage {
27 public:
28     const SortNames &SNames_;
29     PandaUnorderedMap<TypeNum, PandaString> CachedImages_;
30 
TypeImage(const SortNames & names)31     explicit TypeImage(const SortNames &names) : SNames_ {names} {}
32     ~TypeImage() = default;
33 
ImageOfVariance(TypeVariance var)34     PandaString ImageOfVariance(TypeVariance var) const
35     {
36         switch (var) {
37             case TypeVariance::COVARIANT:
38                 return "+";
39             case TypeVariance::CONTRVARIANT:
40                 return "-";
41             case TypeVariance::INVARIANT:
42                 return "~";
43             default:
44                 break;
45         }
46         return "?";
47     }
48 
ImageOfTypeParam(const TypeParam & type_param)49     PandaString ImageOfTypeParam(const TypeParam &type_param)
50     {
51         return ImageOfVariance(type_param.Variance()) + ImageOfType(type_param);
52     }
53 
ImageOfTypeParams(const TypeParams & params)54     PandaString ImageOfTypeParams(const TypeParams &params)
55     {
56         PandaString params_image {""};
57 
58         if (params.size() != 0) {
59             params.ForEach([this, &params_image](const auto &p) {
60                 params_image += PandaString {params_image.empty() ? "( " : ", "};
61                 params_image += ImageOfTypeParam(p);
62             });
63             params_image += " )";
64         }
65 
66         return params_image;
67     }
68 
ImageOfType(const Type & type)69     const PandaString &ImageOfType(const Type &type)
70     {
71         auto cached = CachedImages_.find(type.Number());
72         if (cached != CachedImages_.end()) {
73             return cached->second;
74         }
75 
76         PandaString sort_name {SNames_[type.Sort()]};
77 
78         const auto &params = type.Params();
79 
80         auto &&params_image = ImageOfTypeParams(params);
81 
82         PandaString val = sort_name + params_image;
83 
84         CachedImages_[type.Number()] = val;
85 
86         return CachedImages_[type.Number()];
87     }
88 
89     const PandaString &operator[](const Type &type)
90     {
91         return ImageOfType(type);
92     }
93 };
94 }  // namespace panda::verifier
95 
96 #endif  // VERIFICATION_TYPE_TYPE_IMAGE_H
97