|
| 1 | +//===--- ConstTypeInfo.h - Const Nominal Type Info Structure ----*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_AST_CONST_TYPE_INFO_H |
| 14 | +#define SWIFT_AST_CONST_TYPE_INFO_H |
| 15 | + |
| 16 | +#include <string> |
| 17 | +#include <vector> |
| 18 | +#include <memory> |
| 19 | + |
| 20 | +namespace swift { |
| 21 | +class NominalTypeDecl; |
| 22 | +class VarDecl; |
| 23 | +class Type; |
| 24 | +} // namespace swift |
| 25 | + |
| 26 | +/// Representation of a compile-time-known value, for example |
| 27 | +/// in a type property initializer expression |
| 28 | +class CompileTimeValue { |
| 29 | +public: |
| 30 | + enum ValueKind { RawLiteral, InitCall, Builder, Dictionary, Runtime }; |
| 31 | + |
| 32 | + ValueKind getKind() const { return Kind; } |
| 33 | + |
| 34 | +protected: |
| 35 | + CompileTimeValue(ValueKind ValueKind) : Kind(ValueKind) {} |
| 36 | + |
| 37 | +private: |
| 38 | + ValueKind Kind; |
| 39 | +}; |
| 40 | + |
| 41 | +/// A string representation of a raw literal value, |
| 42 | +/// for example an integer or string or float literal. |
| 43 | +class RawLiteralValue : public CompileTimeValue { |
| 44 | +public: |
| 45 | + RawLiteralValue(std::string Value) |
| 46 | + : CompileTimeValue(ValueKind::RawLiteral), Value(Value) {} |
| 47 | + |
| 48 | + std::string getValue() const { return Value; } |
| 49 | + |
| 50 | + static bool classof(const CompileTimeValue *T) { |
| 51 | + return T->getKind() == ValueKind::RawLiteral; |
| 52 | + } |
| 53 | + |
| 54 | +private: |
| 55 | + std::string Value; |
| 56 | +}; |
| 57 | + |
| 58 | +struct FunctionParameter { |
| 59 | +public: |
| 60 | + std::string getLabel() { return Label; } |
| 61 | + swift::Type *getType() { return Type; } |
| 62 | + |
| 63 | +private: |
| 64 | + std::string Label; |
| 65 | + swift::Type *Type; |
| 66 | +}; |
| 67 | + |
| 68 | +/// A representation of a call to a type's initializer |
| 69 | +/// with a collection of (potentially compile-time-known) parameters |
| 70 | +class InitCallValue : public CompileTimeValue { |
| 71 | +public: |
| 72 | + InitCallValue(std::string Name, std::vector<FunctionParameter> Parameters) |
| 73 | + : CompileTimeValue(ValueKind::InitCall), Name(Name), |
| 74 | + Parameters(Parameters) {} |
| 75 | + |
| 76 | + static bool classof(const CompileTimeValue *T) { |
| 77 | + return T->getKind() == ValueKind::InitCall; |
| 78 | + } |
| 79 | + |
| 80 | + std::string getName() const { return Name; } |
| 81 | + |
| 82 | +private: |
| 83 | + std::string Name; |
| 84 | + std::vector<FunctionParameter> Parameters; |
| 85 | +}; |
| 86 | + |
| 87 | +/// A representation of a Builder pattern initialization expression |
| 88 | +class BuilderValue : public CompileTimeValue { |
| 89 | +public: |
| 90 | + BuilderValue() : CompileTimeValue(ValueKind::Builder) {} |
| 91 | + |
| 92 | + static bool classof(const CompileTimeValue *T) { |
| 93 | + return T->getKind() == ValueKind::Builder; |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | + std::vector<CompileTimeValue> Members; |
| 98 | +}; |
| 99 | + |
| 100 | +/// A dictionary literal value representation |
| 101 | +class DictionaryValue : public CompileTimeValue { |
| 102 | +public: |
| 103 | + DictionaryValue() : CompileTimeValue(ValueKind::Dictionary) {} |
| 104 | + |
| 105 | + static bool classof(const CompileTimeValue *T) { |
| 106 | + return T->getKind() == ValueKind::Dictionary; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +/// A representation of an arbitrary value that does not fall under |
| 111 | +/// any of the above categories. |
| 112 | +class RuntimeValue : public CompileTimeValue { |
| 113 | +public: |
| 114 | + RuntimeValue() : CompileTimeValue(ValueKind::Runtime) {} |
| 115 | + |
| 116 | + static bool classof(const CompileTimeValue *T) { |
| 117 | + return T->getKind() == ValueKind::Runtime; |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +struct ConstValueTypePropertyInfo { |
| 122 | + swift::VarDecl *VarDecl; |
| 123 | + std::shared_ptr<CompileTimeValue> Value; |
| 124 | +}; |
| 125 | + |
| 126 | +struct ConstValueTypeInfo { |
| 127 | + swift::NominalTypeDecl *TypeDecl; |
| 128 | + std::vector<ConstValueTypePropertyInfo> Properties; |
| 129 | +}; |
| 130 | + |
| 131 | +#endif |
0 commit comments