@@ -24,9 +24,10 @@ use crate::backend::ir::ast::{
2424 create_variable_declaration_statement, create_yul_function_definition, create_yul_label,
2525} ;
2626use crate :: backend:: { binder, SemanticAnalysis } ;
27- use crate :: cst:: NodeId ;
27+ use crate :: cst:: { NodeId , TextIndex } ;
2828
2929// __SLANG_DEFINITION_TYPES__ keep in sync with binder
30+ #[ derive( Clone ) ]
3031pub enum Definition {
3132 Constant ( ConstantDefinition ) ,
3233 Contract ( ContractDefinition ) ,
@@ -54,13 +55,13 @@ pub enum Definition {
5455}
5556
5657impl 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" ) ;
58+ pub ( crate ) fn try_create (
59+ definition_id : NodeId ,
60+ semantic : & Rc < SemanticAnalysis > ,
61+ ) -> Option < Self > {
62+ let definition = semantic . binder ( ) . find_definition_by_id ( definition_id) ? ;
6263
63- match definition {
64+ let definition = match definition {
6465 binder:: Definition :: Constant ( constant_definition) => Self :: Constant (
6566 create_constant_definition ( & constant_definition. ir_node , semantic) ,
6667 ) ,
@@ -133,6 +134,140 @@ impl Definition {
133134 binder:: Definition :: YulVariable ( yul_variable_definition) => Self :: YulVariable (
134135 create_yul_identifier ( & yul_variable_definition. ir_node , semantic) ,
135136 ) ,
137+ } ;
138+ Some ( definition)
139+ }
140+
141+ pub fn node_id ( & self ) -> NodeId {
142+ match self {
143+ Definition :: Constant ( constant_definition) => constant_definition. node_id ( ) ,
144+ Definition :: Contract ( contract_definition) => contract_definition. node_id ( ) ,
145+ Definition :: Enum ( enum_definition) => enum_definition. node_id ( ) ,
146+ Definition :: EnumMember ( identifier) => identifier. node_id ( ) ,
147+ Definition :: Error ( error_definition) => error_definition. node_id ( ) ,
148+ Definition :: Event ( event_definition) => event_definition. node_id ( ) ,
149+ Definition :: Function ( function_definition) => function_definition. node_id ( ) ,
150+ Definition :: Import ( path_import) => path_import. node_id ( ) ,
151+ Definition :: ImportedSymbol ( import_deconstruction_symbol) => {
152+ import_deconstruction_symbol. node_id ( )
153+ }
154+ Definition :: Interface ( interface_definition) => interface_definition. node_id ( ) ,
155+ Definition :: Library ( library_definition) => library_definition. node_id ( ) ,
156+ Definition :: Modifier ( function_definition) => function_definition. node_id ( ) ,
157+ Definition :: Parameter ( parameter) => parameter. node_id ( ) ,
158+ Definition :: StateVariable ( state_variable_definition) => {
159+ state_variable_definition. node_id ( )
160+ }
161+ Definition :: Struct ( struct_definition) => struct_definition. node_id ( ) ,
162+ Definition :: StructMember ( struct_member) => struct_member. node_id ( ) ,
163+ Definition :: TypeParameter ( parameter) => parameter. node_id ( ) ,
164+ Definition :: UserDefinedValueType ( user_defined_value_type_definition) => {
165+ user_defined_value_type_definition. node_id ( )
166+ }
167+ Definition :: Variable ( variable_declaration_statement) => {
168+ variable_declaration_statement. node_id ( )
169+ }
170+ Definition :: YulFunction ( yul_function_definition) => yul_function_definition. node_id ( ) ,
171+ Definition :: YulLabel ( yul_label) => yul_label. node_id ( ) ,
172+ Definition :: YulParameter ( identifier) => identifier. node_id ( ) ,
173+ Definition :: YulVariable ( identifier) => identifier. node_id ( ) ,
174+ }
175+ }
176+
177+ pub fn identifier ( & self ) -> Identifier {
178+ match self {
179+ Definition :: Constant ( constant_definition) => constant_definition. name ( ) ,
180+ Definition :: Contract ( contract_definition) => contract_definition. name ( ) ,
181+ Definition :: Enum ( enum_definition) => enum_definition. name ( ) ,
182+ Definition :: EnumMember ( identifier) => Rc :: clone ( identifier) ,
183+ Definition :: Error ( error_definition) => error_definition. name ( ) ,
184+ Definition :: Event ( event_definition) => event_definition. name ( ) ,
185+ Definition :: Function ( function_definition) => {
186+ // functions that are definitions always have a name
187+ function_definition
188+ . name ( )
189+ . expect ( "function definitions have a name" )
190+ }
191+ Definition :: Import ( path_import) => {
192+ // imports that are definition always have a name
193+ path_import
194+ . alias ( )
195+ . expect ( "path import definitions have a name" )
196+ }
197+ Definition :: ImportedSymbol ( import_deconstruction_symbol) => {
198+ import_deconstruction_symbol
199+ . alias ( )
200+ . unwrap_or_else ( || import_deconstruction_symbol. name ( ) )
201+ }
202+ Definition :: Interface ( interface_definition) => interface_definition. name ( ) ,
203+ Definition :: Library ( library_definition) => library_definition. name ( ) ,
204+ Definition :: Modifier ( function_definition) => {
205+ // modifiers always have a name
206+ function_definition. name ( ) . expect ( "modifiers have a name" )
207+ }
208+ Definition :: Parameter ( parameter) => {
209+ // parameters that are definitions always have a name
210+ parameter. name ( ) . expect ( "parameter definitions have a name" )
211+ }
212+ Definition :: StateVariable ( state_variable_definition) => {
213+ state_variable_definition. name ( )
214+ }
215+ Definition :: Struct ( struct_definition) => struct_definition. name ( ) ,
216+ Definition :: StructMember ( struct_member) => struct_member. name ( ) ,
217+ Definition :: TypeParameter ( parameter) => {
218+ // parameters that are definitions always have a name
219+ parameter
220+ . name ( )
221+ . expect ( "type parameter definitions have a name" )
222+ }
223+ Definition :: UserDefinedValueType ( user_defined_value_type_definition) => {
224+ user_defined_value_type_definition. name ( )
225+ }
226+ Definition :: Variable ( variable_declaration_statement) => {
227+ variable_declaration_statement. name ( )
228+ }
229+ Definition :: YulFunction ( yul_function_definition) => yul_function_definition. name ( ) ,
230+ Definition :: YulLabel ( yul_label) => yul_label. label ( ) ,
231+ Definition :: YulParameter ( identifier) => Rc :: clone ( identifier) ,
232+ Definition :: YulVariable ( identifier) => Rc :: clone ( identifier) ,
233+ }
234+ }
235+
236+ pub fn text_offset ( & self ) -> TextIndex {
237+ match self {
238+ Definition :: Constant ( constant_definition) => constant_definition. text_offset ( ) ,
239+ Definition :: Contract ( contract_definition) => contract_definition. text_offset ( ) ,
240+ Definition :: Enum ( enum_definition) => enum_definition. text_offset ( ) ,
241+ Definition :: EnumMember ( identifier) => identifier. text_offset ( ) ,
242+ Definition :: Error ( error_definition) => error_definition. text_offset ( ) ,
243+ Definition :: Event ( event_definition) => event_definition. text_offset ( ) ,
244+ Definition :: Function ( function_definition) => function_definition. text_offset ( ) ,
245+ Definition :: Import ( path_import) => path_import. text_offset ( ) ,
246+ Definition :: ImportedSymbol ( import_deconstruction_symbol) => {
247+ import_deconstruction_symbol. text_offset ( )
248+ }
249+ Definition :: Interface ( interface_definition) => interface_definition. text_offset ( ) ,
250+ Definition :: Library ( library_definition) => library_definition. text_offset ( ) ,
251+ Definition :: Modifier ( function_definition) => function_definition. text_offset ( ) ,
252+ Definition :: Parameter ( parameter) => parameter. text_offset ( ) ,
253+ Definition :: StateVariable ( state_variable_definition) => {
254+ state_variable_definition. text_offset ( )
255+ }
256+ Definition :: Struct ( struct_definition) => struct_definition. text_offset ( ) ,
257+ Definition :: StructMember ( struct_member) => struct_member. text_offset ( ) ,
258+ Definition :: TypeParameter ( parameter) => parameter. text_offset ( ) ,
259+ Definition :: UserDefinedValueType ( user_defined_value_type_definition) => {
260+ user_defined_value_type_definition. text_offset ( )
261+ }
262+ Definition :: Variable ( variable_declaration_statement) => {
263+ variable_declaration_statement. text_offset ( )
264+ }
265+ Definition :: YulFunction ( yul_function_definition) => {
266+ yul_function_definition. text_offset ( )
267+ }
268+ Definition :: YulLabel ( yul_label) => yul_label. text_offset ( ) ,
269+ Definition :: YulParameter ( identifier) => identifier. text_offset ( ) ,
270+ Definition :: YulVariable ( identifier) => identifier. text_offset ( ) ,
136271 }
137272 }
138273
@@ -182,6 +317,9 @@ macro_rules! define_references_method {
182317 pub fn references( & self ) -> Vec <Reference > {
183318 self . semantic. references_binding_to( self . ir_node. node_id)
184319 }
320+ pub fn as_definition( & self ) -> Option <Definition > {
321+ Definition :: try_create( self . ir_node. node_id, & self . semantic)
322+ }
185323 }
186324 }
187325 } ;
0 commit comments