• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 PANDA_ASSEMBLER_META_H
17 #define PANDA_ASSEMBLER_META_H
18 
19 #include <memory>
20 #include <stack>
21 #include <tuple>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <variant>
25 #include <vector>
26 
27 #include "annotation.h"
28 #include "modifiers.h"
29 
30 #include "assembly-type.h"
31 
32 namespace ark::pandasm {
33 
34 class Metadata {
35 public:
36     class Error {
37     public:
38         enum class Type {
39             INVALID_VALUE,
40             MISSING_ATTRIBUTE,
41             MISSING_VALUE,
42             MULTIPLE_ATTRIBUTE,
43             UNEXPECTED_ATTRIBUTE,
44             UNEXPECTED_VALUE,
45             UNKNOWN_ATTRIBUTE
46         };
47 
Error(std::string msg,Type type)48         Error(std::string msg, Type type) : msg_(std::move(msg)), type_(type) {}
49         ~Error() = default;
50         DEFAULT_MOVE_SEMANTIC(Error);
51         DEFAULT_COPY_SEMANTIC(Error);
52 
GetMessage()53         std::string GetMessage() const
54         {
55             return msg_;
56         }
57 
GetType()58         Type GetType() const
59         {
60             return type_;
61         }
62 
63     private:
64         std::string msg_;
65         Type type_;
66     };
67 
68     Metadata() = default;
69 
70     virtual ~Metadata() = default;
71 
SetAttribute(std::string_view attribute)72     std::optional<Error> SetAttribute(std::string_view attribute)
73     {
74         auto err = Validate(attribute);
75         if (err) {
76             return err;
77         }
78 
79         SetFlags(attribute);
80 
81         return Store(attribute);
82     }
83 
RemoveAttribute(const std::string & attribute)84     void RemoveAttribute(const std::string &attribute)
85     {
86         RemoveFlags(attribute);
87 
88         setAttributes_.erase(attribute);
89     }
90 
GetAttribute(const std::string & attribute)91     bool GetAttribute(const std::string &attribute) const
92     {
93         return setAttributes_.find(attribute) != setAttributes_.cend();
94     }
95 
SetAttributeValue(std::string_view attribute,std::string_view value)96     std::optional<Error> SetAttributeValue(std::string_view attribute, std::string_view value)
97     {
98         auto err = Validate(attribute, value);
99         if (err) {
100             return err;
101         }
102 
103         SetFlags(attribute, value);
104 
105         return StoreValue(attribute, value);
106     }
107 
GetAttributeValues(const std::string & attribute)108     std::vector<std::string> GetAttributeValues(const std::string &attribute) const
109     {
110         auto it = attributes_.find(attribute);
111         if (it == attributes_.cend()) {
112             return {};
113         }
114 
115         return it->second;
116     }
117 
GetAttributeValue(const std::string & attribute)118     std::optional<std::string> GetAttributeValue(const std::string &attribute) const
119     {
120         auto values = GetAttributeValues(attribute);
121         if (!values.empty()) {
122             return values.front();
123         }
124 
125         return {};
126     }
127 
GetBoolAttributes()128     const std::unordered_set<std::string> &GetBoolAttributes() const
129     {
130         return setAttributes_;
131     }
132 
GetAttributes()133     const std::unordered_map<std::string, std::vector<std::string>> &GetAttributes() const
134     {
135         return attributes_;
136     }
137 
ValidateData()138     virtual std::optional<Error> ValidateData()
139     {
140         return {};
141     }
142 
143     DEFAULT_COPY_SEMANTIC(Metadata);
144 
145     DEFAULT_MOVE_SEMANTIC(Metadata);
146 
147 protected:
148     virtual std::optional<Error> Validate(std::string_view attribute) const = 0;
149 
150     virtual std::optional<Error> Validate(std::string_view attribute, std::string_view value) const = 0;
151 
StoreValue(std::string_view attribute,std::string_view value)152     virtual std::optional<Error> StoreValue(std::string_view attribute, std::string_view value)
153     {
154         std::string key(attribute);
155         auto it = attributes_.find(key);
156         if (it == attributes_.cend()) {
157             std::tie(it, std::ignore) = attributes_.try_emplace(key);
158         }
159 
160         it->second.emplace_back(value);
161 
162         return {};
163     }
164 
Store(std::string_view attribute)165     virtual std::optional<Error> Store(std::string_view attribute)
166     {
167         setAttributes_.emplace(attribute);
168 
169         return {};
170     }
171 
172     virtual void SetFlags(std::string_view attribute) = 0;
173 
174     virtual void SetFlags(std::string_view attribute, std::string_view value) = 0;
175 
176     virtual void RemoveFlags(std::string_view attribute) = 0;
177 
178     virtual void RemoveFlags(std::string_view attribute, std::string_view value) = 0;
179 
HasAttribute(std::string_view attribute)180     bool HasAttribute(std::string_view attribute) const
181     {
182         std::string key(attribute);
183         return GetAttribute(key) || GetAttributeValue(key);
184     }
185 
186     std::optional<Error> ValidateSize(std::string_view value) const;
187 
188 private:
189     std::unordered_set<std::string> setAttributes_;
190     std::unordered_map<std::string, std::vector<std::string>> attributes_;
191 };
192 
193 class AnnotationMetadata : public Metadata {
194 public:
GetAnnotations()195     const std::vector<AnnotationData> &GetAnnotations() const
196     {
197         return annotations_;
198     }
199 
SetAnnotations(std::vector<AnnotationData> && annotations)200     void SetAnnotations(std::vector<AnnotationData> &&annotations)
201     {
202         ASSERT(annotations_.empty());
203         annotations_ = std::forward<std::vector<AnnotationData>>(annotations);
204     }
205 
AddAnnotations(const std::vector<AnnotationData> & annotations)206     void AddAnnotations(const std::vector<AnnotationData> &annotations)
207     {
208         annotations_.insert(annotations_.end(), annotations.begin(), annotations.end());
209     }
210 
211     std::optional<Error> ValidateData() override;
212 
213 protected:
214     std::optional<Error> Store(std::string_view attribute) override;
215 
216     std::optional<Error> StoreValue(std::string_view attribute, std::string_view value) override;
217 
IsAnnotationRecordAttribute(std::string_view attribute)218     virtual bool IsAnnotationRecordAttribute([[maybe_unused]] std::string_view attribute) const
219     {
220         return false;
221     }
222 
IsAnnotationIdAttribute(std::string_view attribute)223     virtual bool IsAnnotationIdAttribute([[maybe_unused]] std::string_view attribute) const
224     {
225         return false;
226     }
227 
IsAnnotationElementTypeAttribute(std::string_view attribute)228     virtual bool IsAnnotationElementTypeAttribute([[maybe_unused]] std::string_view attribute) const
229     {
230         return false;
231     }
232 
IsAnnotationElementArrayComponentTypeAttribute(std::string_view attribute)233     virtual bool IsAnnotationElementArrayComponentTypeAttribute([[maybe_unused]] std::string_view attribute) const
234     {
235         return false;
236     }
237 
IsAnnotationElementNameAttribute(std::string_view attribute)238     virtual bool IsAnnotationElementNameAttribute([[maybe_unused]] std::string_view attribute) const
239     {
240         return false;
241     }
242 
IsAnnotationElementValueAttribute(std::string_view attribute)243     virtual bool IsAnnotationElementValueAttribute([[maybe_unused]] std::string_view attribute) const
244     {
245         return false;
246     }
247 
248 private:
249     class AnnotationElementBuilder {
250     public:
Initialize(std::string_view name)251         void Initialize(std::string_view name)
252         {
253             name_ = name;
254             isInitialized_ = true;
255         }
256 
Reset()257         void Reset()
258         {
259             name_.clear();
260             values_.clear();
261             type_ = {};
262             componentType_ = {};
263             isInitialized_ = false;
264         }
265 
SetType(Value::Type type)266         void SetType(Value::Type type)
267         {
268             type_ = type;
269         }
270 
SetComponentType(Value::Type type)271         void SetComponentType(Value::Type type)
272         {
273             ASSERT(type != Value::Type::ARRAY);
274             componentType_ = type;
275         }
276 
277         std::optional<Error> AddValue(
278             std::string_view value,
279             const std::unordered_map<std::string, std::unique_ptr<AnnotationData>> &annotationIdMap);
280 
CreateAnnotationElement()281         AnnotationElement CreateAnnotationElement()
282         {
283             if (IsArray()) {
284                 return AnnotationElement(name_,
285                                          std::make_unique<ArrayValue>(componentType_.value(), std::move(values_)));
286             }
287 
288             return AnnotationElement(name_, std::make_unique<ScalarValue>(values_.front()));
289         }
290 
IsArray()291         bool IsArray() const
292         {
293             return type_.value() == Value::Type::ARRAY;
294         }
295 
IsTypeSet()296         bool IsTypeSet() const
297         {
298             return type_.has_value();
299         }
300 
IsComponentTypeSet()301         bool IsComponentTypeSet() const
302         {
303             return componentType_.has_value();
304         }
305 
IsInitialized()306         bool IsInitialized() const
307         {
308             return isInitialized_;
309         }
310 
IsCompleted()311         bool IsCompleted() const
312         {
313             if (!IsTypeSet()) {
314                 return false;
315             }
316 
317             if (IsArray() && !IsComponentTypeSet()) {
318                 return false;
319             }
320 
321             if (!IsArray() && values_.empty()) {
322                 return false;
323             }
324 
325             return true;
326         }
327 
328     private:
329         bool isInitialized_ {false};
330         std::string name_;
331         std::optional<Value::Type> type_;
332         std::optional<Value::Type> componentType_;
333         std::vector<ScalarValue> values_;
334     };
335 
336     class AnnotationBuilder {
337     public:
Initialize(std::string_view name)338         void Initialize(std::string_view name)
339         {
340             name_ = name;
341             isInitialized_ = true;
342         }
343 
Reset()344         void Reset()
345         {
346             name_.clear();
347             elements_.clear();
348             id_ = {};
349             isInitialized_ = false;
350         }
351 
SetId(std::string_view id)352         void SetId(std::string_view id)
353         {
354             id_ = id;
355         }
356 
GetId()357         std::string GetId() const
358         {
359             ASSERT(HasId());
360             return id_.value();
361         }
362 
AddElement(AnnotationElement && element)363         void AddElement(AnnotationElement &&element)
364         {
365             elements_.push_back(std::forward<AnnotationElement>(element));
366         }
367 
CreateAnnotationData()368         std::unique_ptr<AnnotationData> CreateAnnotationData()
369         {
370             return std::make_unique<AnnotationData>(name_, std::move(elements_));
371         };
372 
AddAnnnotationDataToVector(std::vector<AnnotationData> * annotations)373         void AddAnnnotationDataToVector(std::vector<AnnotationData> *annotations)
374         {
375             annotations->emplace_back(name_, std::move(elements_));
376         }
377 
HasId()378         bool HasId() const
379         {
380             return id_.has_value();
381         }
382 
IsInitialized()383         bool IsInitialized() const
384         {
385             return isInitialized_;
386         }
387 
388     private:
389         std::string name_;
390         std::optional<std::string> id_;
391         std::vector<AnnotationElement> elements_;
392         bool isInitialized_ {false};
393     };
394 
395     std::optional<Metadata::Error> MeetExpRecordAttribute(std::string_view attribute, std::string_view value);
396     std::optional<Metadata::Error> MeetExpIdAttribute(std::string_view attribute, std::string_view value);
397     std::optional<Metadata::Error> MeetExpElementNameAttribute(std::string_view attribute, std::string_view value);
398     std::optional<Metadata::Error> MeetExpElementTypeAttribute(std::string_view attribute, std::string_view value);
399     std::optional<Metadata::Error> MeetExpElementArrayComponentTypeAttribute(std::string_view attribute,
400                                                                              std::string_view value);
401     std::optional<Metadata::Error> MeetExpElementValueAttribute(std::string_view attribute, std::string_view value);
402 
GetOrCreateAnnotationElementBuilder()403     AnnotationElementBuilder *GetOrCreateAnnotationElementBuilder() const
404     {
405         if (!annotationElementBuilder_) {
406             annotationElementBuilder_ = std::make_unique<AnnotationElementBuilder>();
407         }
408         return annotationElementBuilder_.get();
409     }
410 
GetOrCreateAnnotationBuilder()411     AnnotationBuilder *GetOrCreateAnnotationBuilder() const
412     {
413         if (!annotationBuilder_) {
414             annotationBuilder_ = std::make_unique<AnnotationBuilder>();
415         }
416         return annotationBuilder_.get();
417     }
418 
InitializeAnnotationBuilder(std::string_view name)419     void InitializeAnnotationBuilder(std::string_view name)
420     {
421         if (IsParseAnnotation()) {
422             ResetAnnotationBuilder();
423         }
424 
425         GetOrCreateAnnotationBuilder()->Initialize(name);
426     }
427 
ResetAnnotationBuilder()428     void ResetAnnotationBuilder()
429     {
430         ASSERT(IsParseAnnotation());
431 
432         if (IsParseAnnotationElement() && GetOrCreateAnnotationElementBuilder()->IsCompleted()) {
433             ResetAnnotationElementBuilder();
434         }
435 
436         if (GetOrCreateAnnotationBuilder()->HasId()) {
437             idMap_.insert(
438                 {GetOrCreateAnnotationBuilder()->GetId(), GetOrCreateAnnotationBuilder()->CreateAnnotationData()});
439         } else {
440             GetOrCreateAnnotationBuilder()->AddAnnnotationDataToVector(&annotations_);
441         }
442 
443         GetOrCreateAnnotationBuilder()->Reset();
444     }
445 
IsParseAnnotation()446     bool IsParseAnnotation() const
447     {
448         return GetOrCreateAnnotationBuilder()->IsInitialized();
449     }
450 
InitializeAnnotationElementBuilder(std::string_view name)451     void InitializeAnnotationElementBuilder(std::string_view name)
452     {
453         if (IsParseAnnotationElement() && GetOrCreateAnnotationElementBuilder()->IsCompleted()) {
454             ResetAnnotationElementBuilder();
455         }
456 
457         GetOrCreateAnnotationElementBuilder()->Initialize(name);
458     }
459 
ResetAnnotationElementBuilder()460     void ResetAnnotationElementBuilder()
461     {
462         ASSERT(IsParseAnnotationElement());
463         ASSERT(GetOrCreateAnnotationElementBuilder()->IsCompleted());
464 
465         GetOrCreateAnnotationBuilder()->AddElement(GetOrCreateAnnotationElementBuilder()->CreateAnnotationElement());
466 
467         GetOrCreateAnnotationElementBuilder()->Reset();
468     }
469 
IsParseAnnotationElement()470     bool IsParseAnnotationElement() const
471     {
472         return GetOrCreateAnnotationElementBuilder()->IsInitialized();
473     }
474 
475     mutable std::unique_ptr<AnnotationBuilder> annotationBuilder_;
476     mutable std::unique_ptr<AnnotationElementBuilder> annotationElementBuilder_;
477     std::vector<AnnotationData> annotations_;
478     std::unordered_map<std::string, std::unique_ptr<AnnotationData>> idMap_;
479 };
480 
481 class ItemMetadata : public AnnotationMetadata {
482 public:
GetAccessFlags()483     uint32_t GetAccessFlags() const
484     {
485         return accessFlags_;
486     }
487 
SetAccessFlags(uint32_t accessFlags)488     void SetAccessFlags(uint32_t accessFlags)
489     {
490         accessFlags_ = accessFlags;
491     }
492 
493     PANDA_PUBLIC_API bool IsForeign() const;
494 
495 private:
496     uint32_t accessFlags_ {0};
497 };
498 
499 class RecordMetadata : public ItemMetadata {
500 public:
501     virtual std::string GetBase() const;
502 
503     virtual std::vector<std::string> GetInterfaces() const;
504 
505     virtual bool IsAnnotation() const;
506 
507     virtual bool IsRuntimeAnnotation() const;
508 
509     virtual bool IsTypeAnnotation() const;
510 
511     virtual bool IsRuntimeTypeAnnotation() const;
512 
513 protected:
514     std::optional<Error> Validate(std::string_view attribute) const override;
515 
516     std::optional<Error> Validate(std::string_view attribute, std::string_view value) const override;
517 
518     void SetFlags(std::string_view attribute) override;
519 
520     void SetFlags(std::string_view attribute, std::string_view value) override;
521 
522     void RemoveFlags(std::string_view attribute) override;
523 
524     void RemoveFlags(std::string_view attribute, std::string_view value) override;
525 };
526 
527 class FieldMetadata : public ItemMetadata {
528 public:
SetFieldType(const Type & type)529     void SetFieldType(const Type &type)
530     {
531         fieldType_ = type;
532     }
533 
GetFieldType()534     Type GetFieldType() const
535     {
536         return fieldType_;
537     }
538 
SetValue(const ScalarValue & value)539     void SetValue(const ScalarValue &value)
540     {
541         value_ = value;
542     }
543 
GetValue()544     std::optional<ScalarValue> GetValue() const
545     {
546         return value_;
547     }
548 
549 protected:
550     std::optional<Error> StoreValue(std::string_view attribute, std::string_view value) override;
551 
552     std::optional<Error> Validate(std::string_view attribute) const override;
553 
554     std::optional<Error> Validate(std::string_view attribute, std::string_view value) const override;
555 
556     void SetFlags(std::string_view attribute) override;
557 
558     void SetFlags(std::string_view attribute, std::string_view value) override;
559 
560     void RemoveFlags(std::string_view attribute) override;
561 
562     void RemoveFlags(std::string_view attribute, std::string_view value) override;
563 
IsValueAttribute(std::string_view attribute)564     virtual bool IsValueAttribute(std::string_view attribute)
565     {
566         return attribute == "value";
567     }
568 
569 private:
570     Type fieldType_;
571     std::optional<ScalarValue> value_;
572 };
573 
574 class FunctionMetadata : public ItemMetadata {
575 public:
576     virtual bool HasImplementation() const;
577 
578     virtual bool IsCtor() const;
579 
580     virtual bool IsCctor() const;
581 
582 protected:
583     std::optional<Error> Validate(std::string_view attribute) const override;
584 
585     std::optional<Error> Validate(std::string_view attribute, std::string_view value) const override;
586 
587     void SetFlags(std::string_view attribute) override;
588 
589     void SetFlags(std::string_view attribute, std::string_view value) override;
590 
591     void RemoveFlags(std::string_view attribute) override;
592 
593     void RemoveFlags(std::string_view attribute, std::string_view value) override;
594 };
595 
596 class ParamMetadata : public AnnotationMetadata {
597 protected:
598     std::optional<Error> Validate(std::string_view attribute) const override;
599 
600     std::optional<Error> Validate(std::string_view attribute, std::string_view value) const override;
601 
602     void SetFlags(std::string_view attribute) override;
603 
604     void SetFlags(std::string_view attribute, std::string_view value) override;
605 
606     void RemoveFlags(std::string_view attribute) override;
607 
608     void RemoveFlags(std::string_view attribute, std::string_view value) override;
609 };
610 
611 }  // namespace ark::pandasm
612 
613 #endif  // PANDA_ASSEMBLER_META_H
614