|
| 1 | +//==--- PropertiesBase.td - Baseline definitions for AST properties -------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +class ASTNode; |
| 10 | + |
| 11 | +/// The type of the property. |
| 12 | +class PropertyType<string typeName = ""> { |
| 13 | + /// The C++ type name for the type. |
| 14 | + string CXXName = !if(!ne(typeName, ""), typeName, NAME); |
| 15 | + |
| 16 | + /// Whether the C++ type should generally be passed around by reference. |
| 17 | + bit PassByReference = 0; |
| 18 | + |
| 19 | + /// Whether `const` should be prepended to the type when writing. |
| 20 | + bit ConstWhenWriting = 0; |
| 21 | + |
| 22 | + /// Given a value of type Optional<CXXName> bound as 'value', yield a |
| 23 | + /// CXXName that can be serialized into a DataStreamTypeWriter. |
| 24 | + string PackOptional = ""; |
| 25 | + |
| 26 | + /// Given a value of type CXXName bound as 'value' that was deserialized |
| 27 | + /// by a DataStreamTypeReader, yield an Optional<CXXName>. |
| 28 | + string UnpackOptional = ""; |
| 29 | + |
| 30 | + /// A list of types for which buffeers must be passed to the read |
| 31 | + /// operations. |
| 32 | + list<PropertyType> BufferElementTypes = []; |
| 33 | +} |
| 34 | + |
| 35 | +/// Property types that correspond to specific C++ enums. |
| 36 | +class EnumPropertyType<string typeName = ""> : PropertyType<typeName> {} |
| 37 | + |
| 38 | +/// Property types that correspond to a specific C++ class. |
| 39 | +/// Supports optional values by using the null representation. |
| 40 | +class RefPropertyType<string className> : PropertyType<className # "*"> { |
| 41 | + let PackOptional = |
| 42 | + "value ? *value : nullptr"; |
| 43 | + let UnpackOptional = |
| 44 | + "value ? llvm::Optional<" # CXXName # ">(value) : llvm::None"; |
| 45 | +} |
| 46 | + |
| 47 | +/// Property types that correspond to a specific subclass of another type. |
| 48 | +class SubclassPropertyType<string className, PropertyType base> |
| 49 | + : RefPropertyType<className> { |
| 50 | + PropertyType Base = base; |
| 51 | + string SubclassName = className; |
| 52 | + let ConstWhenWriting = base.ConstWhenWriting; |
| 53 | +} |
| 54 | + |
| 55 | +/// Property types that support optional values by using their |
| 56 | +/// default value. |
| 57 | +class DefaultValuePropertyType<string typeName = ""> : PropertyType<typeName> { |
| 58 | + let PackOptional = |
| 59 | + "value ? *value : " # CXXName # "()"; |
| 60 | + let UnpackOptional = |
| 61 | + "value.isNull() ? llvm::None : llvm::Optional<" # CXXName # ">(value)"; |
| 62 | +} |
| 63 | + |
| 64 | +/// Property types that correspond to integer types and support optional |
| 65 | +/// values by shifting the value over by 1. |
| 66 | +class CountPropertyType<string typeName = ""> : PropertyType<typeName> { |
| 67 | + let PackOptional = |
| 68 | + "value ? *value + 1 : 0"; |
| 69 | + let UnpackOptional = |
| 70 | + "value ? llvm::Optional<" # CXXName # ">(value - 1) : llvm::None"; |
| 71 | +} |
| 72 | + |
| 73 | +def APInt : PropertyType<"llvm::APInt"> { let PassByReference = 1; } |
| 74 | +def APSInt : PropertyType<"llvm::APSInt"> { let PassByReference = 1; } |
| 75 | +def ArraySizeModifier : EnumPropertyType<"ArrayType::ArraySizeModifier">; |
| 76 | +def AttrKind : EnumPropertyType<"attr::Kind">; |
| 77 | +def AutoTypeKeyword : EnumPropertyType; |
| 78 | +def Bool : PropertyType<"bool">; |
| 79 | +def BuiltinTypeKind : EnumPropertyType<"BuiltinType::Kind">; |
| 80 | +def CallingConv : EnumPropertyType; |
| 81 | +def DeclarationName : PropertyType; |
| 82 | +def DeclarationNameKind : EnumPropertyType<"DeclarationName::NameKind">; |
| 83 | +def DeclRef : RefPropertyType<"Decl"> { let ConstWhenWriting = 1; } |
| 84 | + def CXXRecordDeclRef : |
| 85 | + SubclassPropertyType<"CXXRecordDecl", DeclRef>; |
| 86 | + def FunctionDeclRef : |
| 87 | + SubclassPropertyType<"FunctionDecl", DeclRef>; |
| 88 | + def NamedDeclRef : |
| 89 | + SubclassPropertyType<"NamedDecl", DeclRef>; |
| 90 | + def NamespaceDeclRef : |
| 91 | + SubclassPropertyType<"NamespaceDecl", DeclRef>; |
| 92 | + def NamespaceAliasDeclRef : |
| 93 | + SubclassPropertyType<"NamespaceAliasDecl", DeclRef>; |
| 94 | + def ObjCProtocolDeclRef : |
| 95 | + SubclassPropertyType<"ObjCProtocolDecl", DeclRef>; |
| 96 | + def ObjCTypeParamDeclRef : |
| 97 | + SubclassPropertyType<"ObjCTypeParamDecl", DeclRef>; |
| 98 | + def TagDeclRef : |
| 99 | + SubclassPropertyType<"TagDecl", DeclRef>; |
| 100 | + def TemplateDeclRef : |
| 101 | + SubclassPropertyType<"TemplateDecl", DeclRef>; |
| 102 | + def TemplateTypeParmDeclRef : |
| 103 | + SubclassPropertyType<"TemplateTypeParmDecl", DeclRef>; |
| 104 | + def TemplateTemplateParmDeclRef : |
| 105 | + SubclassPropertyType<"TemplateTemplateParmDecl", DeclRef>; |
| 106 | + def ValueDeclRef : |
| 107 | + SubclassPropertyType<"ValueDecl", DeclRef>; |
| 108 | +def ElaboratedTypeKeyword : EnumPropertyType; |
| 109 | +def ExtParameterInfo : PropertyType<"FunctionProtoType::ExtParameterInfo">; |
| 110 | +def Identifier : PropertyType<"IdentifierInfo*">; |
| 111 | +def NestedNameSpecifier : PropertyType<"NestedNameSpecifier *">; |
| 112 | +def NestedNameSpecifierKind : |
| 113 | + EnumPropertyType<"NestedNameSpecifier::SpecifierKind">; |
| 114 | +def OverloadedOperatorKind : EnumPropertyType; |
| 115 | +def Qualifiers : PropertyType; |
| 116 | +def QualType : DefaultValuePropertyType; |
| 117 | +def RefQualifierKind : EnumPropertyType; |
| 118 | +def Selector : PropertyType; |
| 119 | +def SourceLocation : PropertyType; |
| 120 | +def StmtRef : RefPropertyType<"Stmt"> { let ConstWhenWriting = 1; } |
| 121 | + def ExprRef : SubclassPropertyType<"Expr", StmtRef>; |
| 122 | +def TemplateArgument : PropertyType; |
| 123 | +def TemplateArgumentKind : EnumPropertyType<"TemplateArgument::ArgKind">; |
| 124 | +def TemplateName : DefaultValuePropertyType; |
| 125 | +def TemplateNameKind : EnumPropertyType<"TemplateName::NameKind">; |
| 126 | +def UInt32 : CountPropertyType<"uint32_t">; |
| 127 | +def UInt64 : CountPropertyType<"uint64_t">; |
| 128 | +def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; |
| 129 | +def VectorKind : EnumPropertyType<"VectorType::VectorKind">; |
| 130 | + |
| 131 | +def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { |
| 132 | + let BufferElementTypes = [ QualType ]; |
| 133 | +} |
| 134 | + |
| 135 | +/// Arrays. The corresponding C++ type is ArrayRef of the corresponding |
| 136 | +/// C++ type of the element. |
| 137 | +class Array<PropertyType element> : PropertyType { |
| 138 | + PropertyType Element = element; |
| 139 | + let BufferElementTypes = [ element ]; |
| 140 | +} |
| 141 | + |
| 142 | +/// llvm::Optional<T>. The corresponding C++ type is generally just the |
| 143 | +/// corresponding C++ type of the element. |
| 144 | +/// |
| 145 | +/// Optional<Unsigned> may restrict the range of the operand for some |
| 146 | +/// serialization clients. |
| 147 | +class Optional<PropertyType element> : PropertyType { |
| 148 | + PropertyType Element = element; |
| 149 | + let PassByReference = element.PassByReference; |
| 150 | +} |
| 151 | + |
| 152 | +/// A property of an AST node. |
| 153 | +class Property<string name, PropertyType type> { |
| 154 | + ASTNode Class; |
| 155 | + string Name = name; |
| 156 | + PropertyType Type = type; |
| 157 | + |
| 158 | + /// A function for reading the property, expressed in terms of a variable |
| 159 | + /// "node". |
| 160 | + code Read; |
| 161 | +} |
| 162 | + |
| 163 | +/// A rule for creating objects of this type. |
| 164 | +class Creator<code create> { |
| 165 | + ASTNode Class; |
| 166 | + |
| 167 | + /// A function for creating values of this kind, expressed in terms of a |
| 168 | + /// variable `ctx` of type `ASTContext &`. Must also refer to all of the |
| 169 | + /// properties by name. |
| 170 | + code Create = create; |
| 171 | +} |
| 172 | + |
| 173 | +/// A rule which overrides some of the normal rules. |
| 174 | +class Override { |
| 175 | + ASTNode Class; |
| 176 | + |
| 177 | + /// Properties from base classes that should be ignored. |
| 178 | + list<string> IgnoredProperties = []; |
| 179 | +} |
0 commit comments