|
| 1 | +use std::rc::Rc; |
| 2 | + |
| 3 | +use paste::paste; |
| 4 | + |
| 5 | +use super::super::nodes::{ |
| 6 | + ConstantDefinition, ConstantDefinitionStruct, ContractDefinition, ContractDefinitionStruct, |
| 7 | + EnumDefinition, EnumDefinitionStruct, ErrorDefinition, ErrorDefinitionStruct, EventDefinition, |
| 8 | + EventDefinitionStruct, FunctionDefinition, FunctionDefinitionStruct, |
| 9 | + ImportDeconstructionSymbol, ImportDeconstructionSymbolStruct, InterfaceDefinition, |
| 10 | + InterfaceDefinitionStruct, LibraryDefinition, LibraryDefinitionStruct, Parameter, |
| 11 | + ParameterStruct, PathImport, PathImportStruct, StateVariableDefinition, |
| 12 | + StateVariableDefinitionStruct, StructDefinition, StructDefinitionStruct, StructMember, |
| 13 | + StructMemberStruct, UserDefinedValueTypeDefinition, UserDefinedValueTypeDefinitionStruct, |
| 14 | + VariableDeclarationStatement, VariableDeclarationStatementStruct, YulFunctionDefinition, |
| 15 | + YulFunctionDefinitionStruct, YulLabel, YulLabelStruct, |
| 16 | +}; |
| 17 | +use super::{create_identifier, create_yul_identifier, Identifier, Reference, YulIdentifier}; |
| 18 | +use crate::backend::ir::ast::{ |
| 19 | + create_constant_definition, create_contract_definition, create_enum_definition, |
| 20 | + create_error_definition, create_event_definition, create_function_definition, |
| 21 | + create_import_deconstruction_symbol, create_interface_definition, create_library_definition, |
| 22 | + create_parameter, create_path_import, create_state_variable_definition, |
| 23 | + create_struct_definition, create_struct_member, create_user_defined_value_type_definition, |
| 24 | + create_variable_declaration_statement, create_yul_function_definition, create_yul_label, |
| 25 | +}; |
| 26 | +use crate::backend::{binder, SemanticAnalysis}; |
| 27 | +use crate::cst::NodeId; |
| 28 | + |
| 29 | +// __SLANG_DEFINITION_TYPES__ keep in sync with binder |
| 30 | +pub enum Definition { |
| 31 | + Constant(ConstantDefinition), |
| 32 | + Contract(ContractDefinition), |
| 33 | + Enum(EnumDefinition), |
| 34 | + EnumMember(Identifier), |
| 35 | + Error(ErrorDefinition), |
| 36 | + Event(EventDefinition), |
| 37 | + Function(FunctionDefinition), |
| 38 | + Import(PathImport), |
| 39 | + ImportedSymbol(ImportDeconstructionSymbol), |
| 40 | + Interface(InterfaceDefinition), |
| 41 | + Library(LibraryDefinition), |
| 42 | + Modifier(FunctionDefinition), |
| 43 | + Parameter(Parameter), |
| 44 | + StateVariable(StateVariableDefinition), |
| 45 | + Struct(StructDefinition), |
| 46 | + StructMember(StructMember), |
| 47 | + TypeParameter(Parameter), |
| 48 | + UserDefinedValueType(UserDefinedValueTypeDefinition), |
| 49 | + Variable(VariableDeclarationStatement), |
| 50 | + YulFunction(YulFunctionDefinition), |
| 51 | + YulLabel(YulLabel), |
| 52 | + YulParameter(YulIdentifier), |
| 53 | + YulVariable(YulIdentifier), |
| 54 | +} |
| 55 | + |
| 56 | +impl Definition { |
| 57 | + pub(crate) fn create(definition_id: NodeId, semantic: &Rc<SemanticAnalysis>) -> Self { |
| 58 | + let definition = semantic |
| 59 | + .binder() |
| 60 | + .find_definition_by_id(definition_id) |
| 61 | + .expect("definition_id references a definition node"); |
| 62 | + |
| 63 | + match definition { |
| 64 | + binder::Definition::Constant(constant_definition) => Self::Constant( |
| 65 | + create_constant_definition(&constant_definition.ir_node, semantic), |
| 66 | + ), |
| 67 | + binder::Definition::Contract(contract_definition) => Self::Contract( |
| 68 | + create_contract_definition(&contract_definition.ir_node, semantic), |
| 69 | + ), |
| 70 | + binder::Definition::Enum(enum_definition) => { |
| 71 | + Self::Enum(create_enum_definition(&enum_definition.ir_node, semantic)) |
| 72 | + } |
| 73 | + binder::Definition::EnumMember(enum_member_definition) => { |
| 74 | + Self::EnumMember(create_identifier(&enum_member_definition.ir_node, semantic)) |
| 75 | + } |
| 76 | + binder::Definition::Error(error_definition) => { |
| 77 | + Self::Error(create_error_definition(&error_definition.ir_node, semantic)) |
| 78 | + } |
| 79 | + binder::Definition::Event(event_definition) => { |
| 80 | + Self::Event(create_event_definition(&event_definition.ir_node, semantic)) |
| 81 | + } |
| 82 | + binder::Definition::Function(function_definition) => Self::Function( |
| 83 | + create_function_definition(&function_definition.ir_node, semantic), |
| 84 | + ), |
| 85 | + binder::Definition::Import(import_definition) => { |
| 86 | + Self::Import(create_path_import(&import_definition.ir_node, semantic)) |
| 87 | + } |
| 88 | + binder::Definition::ImportedSymbol(imported_symbol_definition) => Self::ImportedSymbol( |
| 89 | + create_import_deconstruction_symbol(&imported_symbol_definition.ir_node, semantic), |
| 90 | + ), |
| 91 | + binder::Definition::Interface(interface_definition) => Self::Interface( |
| 92 | + create_interface_definition(&interface_definition.ir_node, semantic), |
| 93 | + ), |
| 94 | + binder::Definition::Library(library_definition) => Self::Library( |
| 95 | + create_library_definition(&library_definition.ir_node, semantic), |
| 96 | + ), |
| 97 | + binder::Definition::Modifier(modifier_definition) => Self::Modifier( |
| 98 | + create_function_definition(&modifier_definition.ir_node, semantic), |
| 99 | + ), |
| 100 | + binder::Definition::Parameter(parameter_definition) => { |
| 101 | + Self::Parameter(create_parameter(¶meter_definition.ir_node, semantic)) |
| 102 | + } |
| 103 | + binder::Definition::StateVariable(state_variable_definition) => Self::StateVariable( |
| 104 | + create_state_variable_definition(&state_variable_definition.ir_node, semantic), |
| 105 | + ), |
| 106 | + binder::Definition::Struct(struct_definition) => Self::Struct( |
| 107 | + create_struct_definition(&struct_definition.ir_node, semantic), |
| 108 | + ), |
| 109 | + binder::Definition::StructMember(struct_member_definition) => Self::StructMember( |
| 110 | + create_struct_member(&struct_member_definition.ir_node, semantic), |
| 111 | + ), |
| 112 | + binder::Definition::TypeParameter(type_parameter_definition) => Self::TypeParameter( |
| 113 | + create_parameter(&type_parameter_definition.ir_node, semantic), |
| 114 | + ), |
| 115 | + binder::Definition::UserDefinedValueType(user_defined_value_type_definition) => { |
| 116 | + Self::UserDefinedValueType(create_user_defined_value_type_definition( |
| 117 | + &user_defined_value_type_definition.ir_node, |
| 118 | + semantic, |
| 119 | + )) |
| 120 | + } |
| 121 | + binder::Definition::Variable(variable_definition) => Self::Variable( |
| 122 | + create_variable_declaration_statement(&variable_definition.ir_node, semantic), |
| 123 | + ), |
| 124 | + binder::Definition::YulFunction(yul_function_definition) => Self::YulFunction( |
| 125 | + create_yul_function_definition(&yul_function_definition.ir_node, semantic), |
| 126 | + ), |
| 127 | + binder::Definition::YulLabel(yul_label_definition) => { |
| 128 | + Self::YulLabel(create_yul_label(&yul_label_definition.ir_node, semantic)) |
| 129 | + } |
| 130 | + binder::Definition::YulParameter(yul_parameter_definition) => Self::YulParameter( |
| 131 | + create_yul_identifier(&yul_parameter_definition.ir_node, semantic), |
| 132 | + ), |
| 133 | + binder::Definition::YulVariable(yul_variable_definition) => Self::YulVariable( |
| 134 | + create_yul_identifier(&yul_variable_definition.ir_node, semantic), |
| 135 | + ), |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + pub fn references(&self) -> Vec<Reference> { |
| 140 | + match self { |
| 141 | + Definition::Constant(constant_definition) => constant_definition.references(), |
| 142 | + Definition::Contract(contract_definition) => contract_definition.references(), |
| 143 | + Definition::Enum(enum_definition) => enum_definition.references(), |
| 144 | + Definition::EnumMember(identifier) => identifier.references(), |
| 145 | + Definition::Error(error_definition) => error_definition.references(), |
| 146 | + Definition::Event(event_definition) => event_definition.references(), |
| 147 | + Definition::Function(function_definition) => function_definition.references(), |
| 148 | + Definition::Import(path_import) => path_import.references(), |
| 149 | + Definition::ImportedSymbol(import_deconstruction_symbol) => { |
| 150 | + import_deconstruction_symbol.references() |
| 151 | + } |
| 152 | + Definition::Interface(interface_definition) => interface_definition.references(), |
| 153 | + Definition::Library(library_definition) => library_definition.references(), |
| 154 | + Definition::Modifier(function_definition) => function_definition.references(), |
| 155 | + Definition::Parameter(parameter) => parameter.references(), |
| 156 | + Definition::StateVariable(state_variable_definition) => { |
| 157 | + state_variable_definition.references() |
| 158 | + } |
| 159 | + Definition::Struct(struct_definition) => struct_definition.references(), |
| 160 | + Definition::StructMember(struct_member) => struct_member.references(), |
| 161 | + Definition::TypeParameter(parameter) => parameter.references(), |
| 162 | + Definition::UserDefinedValueType(user_defined_value_type_definition) => { |
| 163 | + user_defined_value_type_definition.references() |
| 164 | + } |
| 165 | + Definition::Variable(variable_declaration_statement) => { |
| 166 | + variable_declaration_statement.references() |
| 167 | + } |
| 168 | + Definition::YulFunction(yul_function_definition) => { |
| 169 | + yul_function_definition.references() |
| 170 | + } |
| 171 | + Definition::YulLabel(yul_label) => yul_label.references(), |
| 172 | + Definition::YulParameter(identifier) => identifier.references(), |
| 173 | + Definition::YulVariable(identifier) => identifier.references(), |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +macro_rules! define_references_method { |
| 179 | + ($type:ident) => { |
| 180 | + paste! { |
| 181 | + impl [<$type Struct>] { |
| 182 | + pub fn references(&self) -> Vec<Reference> { |
| 183 | + self.semantic.references_binding_to(self.ir_node.node_id) |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + }; |
| 188 | +} |
| 189 | + |
| 190 | +define_references_method!(ConstantDefinition); |
| 191 | +define_references_method!(ContractDefinition); |
| 192 | +define_references_method!(EnumDefinition); |
| 193 | +define_references_method!(ErrorDefinition); |
| 194 | +define_references_method!(EventDefinition); |
| 195 | +define_references_method!(FunctionDefinition); |
| 196 | +define_references_method!(ImportDeconstructionSymbol); |
| 197 | +define_references_method!(InterfaceDefinition); |
| 198 | +define_references_method!(LibraryDefinition); |
| 199 | +define_references_method!(Parameter); |
| 200 | +define_references_method!(PathImport); |
| 201 | +define_references_method!(StateVariableDefinition); |
| 202 | +define_references_method!(StructDefinition); |
| 203 | +define_references_method!(StructMember); |
| 204 | +define_references_method!(UserDefinedValueTypeDefinition); |
| 205 | +define_references_method!(VariableDeclarationStatement); |
| 206 | +define_references_method!(YulLabel); |
| 207 | +define_references_method!(YulFunctionDefinition); |
0 commit comments