Skip to content

Commit 61e896f

Browse files
committed
feat: implement callable
Signed-off-by: Rithul Kamesh <[email protected]>
1 parent 550839a commit 61e896f

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/entities/callable.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use super::{
2+
RustVisibility,
3+
attr::RustAttribute,
4+
callsite::CallSite,
5+
lifetime::RustLifetimeParam,
6+
param::{RustGenericParam, RustParameter},
7+
rtype::RustType,
8+
safety::SafetyAnalysis,
9+
variables::RustVariableDeclaration,
10+
};
11+
12+
/// Represents a Rust function or method.
13+
#[derive(Debug, Clone)]
14+
pub struct RustCallable {
15+
/// The name of the function or method.
16+
pub name: String,
17+
/// Visibility of the function (e.g., public, private).
18+
pub visibility: RustVisibility,
19+
/// Documentation comment associated with the function.
20+
pub doc_comment: Option<String>,
21+
/// Attributes attached to the function.
22+
pub attributes: Vec<RustAttribute>,
23+
/// Parameters of the function.
24+
pub parameters: Vec<RustParameter>,
25+
/// Return type of the function.
26+
pub return_type: Option<RustType>,
27+
/// Indicates if the function is asynchronous.
28+
pub is_async: bool,
29+
/// Indicates if the function is a const function.
30+
pub is_const: bool,
31+
/// Indicates if the function is marked as unsafe.
32+
pub is_unsafe: bool,
33+
/// Indicates if the function is declared as extern.
34+
pub is_extern: bool,
35+
/// ABI for the extern function.
36+
pub extern_abi: Option<String>,
37+
/// Generic parameters for the function.
38+
pub generic_params: Vec<RustGenericParam>,
39+
/// Lifetime parameters for the function.
40+
pub lifetime_params: Vec<RustLifetimeParam>,
41+
/// Where clauses associated with the function.
42+
pub where_clauses: Vec<String>,
43+
/// The source code of the function.
44+
pub code: String,
45+
/// The starting line number of the function definition.
46+
pub start_line: usize,
47+
/// The ending line number of the function definition.
48+
pub end_line: usize,
49+
/// List of types referenced in the function.
50+
pub referenced_types: Vec<String>,
51+
/// Variables accessed within the function.
52+
pub accessed_variables: Vec<String>,
53+
/// Call sites present in the function.
54+
pub call_sites: Vec<CallSite>,
55+
/// Variable declarations within the function.
56+
pub variable_declarations: Vec<RustVariableDeclaration>,
57+
/// Cyclomatic complexity metric of the function.
58+
pub cyclomatic_complexity: Option<usize>,
59+
/// Safety analysis information.
60+
pub safety_analysis: SafetyAnalysis,
61+
}
62+
63+
impl RustCallable {
64+
/// Creates a new instance of `RustCallable` with the required fields.
65+
///
66+
/// # Arguments
67+
///
68+
/// * `name` - The name of the function.
69+
/// * `code` - The source code of the function.
70+
/// * `start_line` - The starting line number of the function definition.
71+
/// * `end_line` - The ending line number of the function definition.
72+
/// * `safety_analysis` - The safety analysis details for the function.
73+
///
74+
/// # Example
75+
///
76+
/// ```
77+
/// use crate::entities::callable::RustCallable;
78+
/// use crate::entities::RustVisibility;
79+
/// use crate::entities::SafetyAnalysis;
80+
///
81+
/// let safety = SafetyAnalysis::default(); // Assuming a default implementation is available.
82+
/// let callable = RustCallable::new("my_function".to_string(), "fn my_function() {}".to_string(), 1, 3, safety);
83+
/// ```
84+
pub fn new(
85+
name: String,
86+
code: String,
87+
start_line: usize,
88+
end_line: usize,
89+
safety_analysis: SafetyAnalysis,
90+
) -> Self {
91+
Self {
92+
name,
93+
visibility: RustVisibility::Private,
94+
doc_comment: None,
95+
attributes: Vec::new(),
96+
parameters: Vec::new(),
97+
return_type: None,
98+
is_async: false,
99+
is_const: false,
100+
is_unsafe: false,
101+
is_extern: false,
102+
extern_abi: None,
103+
generic_params: Vec::new(),
104+
lifetime_params: Vec::new(),
105+
where_clauses: Vec::new(),
106+
code,
107+
start_line,
108+
end_line,
109+
referenced_types: Vec::new(),
110+
accessed_variables: Vec::new(),
111+
call_sites: Vec::new(),
112+
variable_declarations: Vec::new(),
113+
cyclomatic_complexity: None,
114+
safety_analysis,
115+
}
116+
}
117+
}

src/entities/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod attr;
2+
pub mod callable;
23
pub mod callsite;
34
pub mod lifetime;
45
pub mod param;
@@ -102,3 +103,19 @@ impl UnsafeReason {
102103
}
103104
}
104105
}
106+
107+
/// Represents different kinds of Rust structs.
108+
///
109+
/// This enumeration distinguishes between the three forms of structs in Rust:
110+
/// - `Normal`: A regular struct with named fields.
111+
/// - `Tuple`: A tuple struct with unnamed fields.
112+
/// - `Unit`: A unit struct without any fields.
113+
#[derive(Debug, Clone, PartialEq)]
114+
pub enum RustStructKind {
115+
/// Regular struct with named fields.
116+
Normal,
117+
/// Tuple struct with unnamed fields.
118+
Tuple,
119+
/// Unit struct without any fields.
120+
Unit,
121+
}

0 commit comments

Comments
 (0)