use std::collections::BTreeSet;
const RUNTIME_END_MARKER: &str = "End BuildLang Runtime";
const C_KEYWORDS: &[&str] = &[
"if",
"else",
"while",
"for",
"do",
"switch",
"case",
"return",
"sizeof",
"goto",
"break",
"continue",
"default",
"typedef",
"struct",
"union",
"enum",
"static",
"const",
"volatile",
"extern",
"inline",
"register",
"restrict",
"_Alignof",
"_Static_assert",
"_Generic",
"alignof",
"__attribute__",
"__declspec",
"defined",
];
const C_STDLIB: &[&str] = &[
"printf",
"fprintf",
"sprintf",
"snprintf",
"vprintf",
"vfprintf",
"vsnprintf",
"puts",
"fputs",
"fputc",
"putchar",
"fflush",
"fopen",
"fclose",
"fread",
"fwrite",
"fgets",
"getchar",
"fgetc",
"fseek",
"ftell",
"rewind",
"perror",
"remove",
"rename",
"setvbuf",
"fileno",
"_fileno",
"_setmode",
"scanf",
"sscanf",
"fscanf",
"malloc",
"calloc",
"realloc",
"free",
"exit",
"abort",
"atexit",
"atoi",
"atol",
"atoll",
"atof",
"strtol",
"strtoll",
"strtoul",
"strtoull",
"strtod",
"strtof",
"qsort",
"bsearch",
"rand",
"srand",
"getenv",
"system",
"labs",
"llabs",
"div",
"memcpy",
"memmove",
"memset",
"memcmp",
"memchr",
"strlen",
"strcmp",
"strncmp",
"strcpy",
"strncpy",
"strcat",
"strncat",
"strchr",
"strrchr",
"strstr",
"strdup",
"strtok",
"strerror",
"sqrt",
"sqrtf",
"pow",
"powf",
"exp",
"expf",
"exp2",
"log",
"logf",
"log2",
"log10",
"sin",
"sinf",
"cos",
"cosf",
"tan",
"tanf",
"asin",
"acos",
"atan",
"atan2",
"sinh",
"cosh",
"tanh",
"floor",
"floorf",
"ceil",
"ceilf",
"round",
"roundf",
"trunc",
"truncf",
"fabs",
"fabsf",
"fmod",
"fmodf",
"fmin",
"fma",
"fmax",
"fminf",
"fmaxf",
"cbrt",
"hypot",
"isnan",
"isinf",
"isfinite",
"copysign",
"abs",
"nextafter",
"ldexp",
"frexp",
"modf",
"setjmp",
"longjmp",
"assert",
"va_start",
"va_end",
"va_arg",
"va_copy",
"time",
"clock",
"isdigit",
"isalpha",
"isspace",
"isalnum",
"isupper",
"islower",
"toupper",
"tolower",
"Sleep",
"GetTickCount64",
"QueryPerformanceCounter",
"QueryPerformanceFrequency",
"WSAStartup",
"WSACleanup",
"socket",
"connect",
"send",
"recv",
"closesocket",
"close",
"getaddrinfo",
"freeaddrinfo",
"htons",
"inet_pton",
"usleep",
"opendir",
"readdir",
"closedir",
"stat",
"_stat",
"mkdir",
"_mkdir",
"isatty",
"_isatty",
"write",
"read",
"CreateThread",
"CloseHandle",
"GetSystemInfo",
"InitializeCriticalSection",
"InitializeConditionVariable",
"EnterCriticalSection",
"LeaveCriticalSection",
"SleepConditionVariableCS",
"WakeAllConditionVariable",
"pthread_create",
"pthread_detach",
"pthread_mutex_lock",
"pthread_mutex_unlock",
"pthread_cond_wait",
"pthread_cond_broadcast",
"sysconf",
];
#[derive(Debug, Clone, PartialEq, Eq)]
enum Tok<'a> {
Ident(&'a str),
Punct(char),
Directive(&'a str),
}
fn tokenize(src: &str) -> Vec<Tok<'_>> {
let bytes = src.as_bytes();
let mut out = Vec::new();
let mut i = 0;
let mut line_start = true;
while i < bytes.len() {
let c = bytes[i];
if c == b'\n' {
line_start = true;
i += 1;
continue;
}
if c.is_ascii_whitespace() {
i += 1;
continue;
}
if c == b'/' && bytes.get(i + 1) == Some(&b'/') {
while i < bytes.len() && bytes[i] != b'\n' {
i += 1;
}
continue;
}
if c == b'/' && bytes.get(i + 1) == Some(&b'*') {
i += 2;
while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
i += 1;
}
i += 2;
continue;
}
if c == b'"' || c == b'\'' {
let quote = c;
i += 1;
while i < bytes.len() && bytes[i] != quote {
if bytes[i] == b'\\' {
i += 1;
}
i += 1;
}
i += 1;
line_start = false;
continue;
}
if c == b'#' && line_start {
let mut j = i + 1;
while j < bytes.len() && (bytes[j] == b' ' || bytes[j] == b'\t') {
j += 1;
}
let start = j;
while j < bytes.len() && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'_') {
j += 1;
}
out.push(Tok::Directive(&src[start..j]));
i = j;
line_start = false;
continue;
}
line_start = false;
if c.is_ascii_alphabetic() || c == b'_' {
let start = i;
while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
i += 1;
}
out.push(Tok::Ident(&src[start..i]));
continue;
}
if c.is_ascii_digit() {
while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'.') {
i += 1;
}
continue;
}
out.push(Tok::Punct(c as char));
i += 1;
}
out
}
fn is_keyword(name: &str) -> bool {
C_KEYWORDS.contains(&name)
}
fn declared_names(toks: &[Tok<'_>]) -> BTreeSet<String> {
let mut declared = BTreeSet::new();
for (k, tok) in toks.iter().enumerate() {
let Tok::Ident(name) = tok else { continue };
if !matches!(toks.get(k + 1), Some(Tok::Punct('('))) {
continue;
}
let prev = if k == 0 { None } else { toks.get(k - 1) };
let is_decl = match prev {
Some(Tok::Ident(p)) => !is_keyword(p) || matches!(*p, "struct" | "static" | "const"),
Some(Tok::Punct('*')) => {
matches!(toks.get(k.wrapping_sub(2)), Some(Tok::Ident(t)) if !is_keyword(t))
}
Some(Tok::Directive("define")) => true,
_ => false,
};
if is_decl {
declared.insert((*name).to_string());
}
}
for (k, tok) in toks.iter().enumerate() {
if let Tok::Directive("define") = tok {
if let Some(Tok::Ident(name)) = toks.get(k + 1) {
declared.insert((*name).to_string());
}
}
}
declared
}
fn pasted_prefixes(toks: &[Tok<'_>]) -> Vec<String> {
let mut prefixes = Vec::new();
for w in toks.windows(4) {
if let [Tok::Ident(p), Tok::Punct('#'), Tok::Punct('#'), Tok::Ident(_)] = w {
prefixes.push((*p).to_string());
}
}
prefixes
}
fn called_names(toks: &[Tok<'_>]) -> BTreeSet<String> {
let mut called = BTreeSet::new();
for (k, tok) in toks.iter().enumerate() {
let Tok::Ident(name) = tok else { continue };
if is_keyword(name) || !matches!(toks.get(k + 1), Some(Tok::Punct('('))) {
continue;
}
if matches!(toks.get(k + 2), Some(Tok::Punct('*'))) {
continue;
}
let prev = if k == 0 { None } else { toks.get(k - 1) };
let looks_like_decl = match prev {
Some(Tok::Ident(p)) => !is_keyword(p),
Some(Tok::Directive(_)) => true,
Some(Tok::Punct('*')) => {
matches!(toks.get(k.wrapping_sub(2)), Some(Tok::Ident(t)) if !is_keyword(t))
}
_ => false,
};
let is_member = matches!(prev, Some(Tok::Punct('.')))
|| (matches!(prev, Some(Tok::Punct('>')))
&& matches!(toks.get(k.wrapping_sub(2)), Some(Tok::Punct('-'))));
if !looks_like_decl && !is_member {
called.insert((*name).to_string());
}
}
called
}
fn variable_names(toks: &[Tok<'_>]) -> BTreeSet<String> {
let mut vars = BTreeSet::new();
for (k, tok) in toks.iter().enumerate() {
if let Tok::Ident(name) = tok {
let next = toks.get(k + 1);
if matches!(
k.checked_sub(1).and_then(|p| toks.get(p)),
Some(Tok::Punct('*'))
) && matches!(
k.checked_sub(2).and_then(|p| toks.get(p)),
Some(Tok::Punct('('))
) && matches!(next, Some(Tok::Punct(')')))
{
vars.insert((*name).to_string());
}
}
}
vars
}
pub fn undeclared_calls(c_source: &str) -> Vec<String> {
let user_start = c_source
.find(RUNTIME_END_MARKER)
.map(|pos| c_source[pos..].find('\n').map_or(pos, |nl| pos + nl))
.unwrap_or(0);
let all_toks = tokenize(c_source);
let user_toks = tokenize(&c_source[user_start..]);
let declared = declared_names(&all_toks);
let vars = variable_names(&all_toks);
let prefixes = pasted_prefixes(&all_toks);
called_names(&user_toks)
.into_iter()
.filter(|n| {
!declared.contains(n)
&& !vars.contains(n)
&& !C_STDLIB.contains(&n.as_str())
&& !prefixes.iter().any(|p| n.starts_with(p.as_str()))
})
.collect()
}
pub fn hint_for(name: &str) -> String {
match name {
"iter" | "into_iter" | "iter_mut" => "iterators are not supported by the C backend yet; \
loop with an index: `let mut i = 0; while i < v.len() { let x = v[i]; ... i += 1; }`"
.to_string(),
"parse" => "`parse` works on a string with its target type written: \
`s.parse::<i32>()` gives a `Result<i32, str>`"
.to_string(),
"unwrap" | "expect" => format!(
"`{name}` works on an `Option` or a `Result` whose type is known here; \
annotate the value (`let r: Result<i32, str> = ...;`) or use `match`"
),
"split" | "lines" | "split_whitespace" | "chars" | "trim" | "bytes" => format!(
"`{name}` is not supported by the C backend yet; walk the string by index with \
`s.len()` and `s[i]`"
),
"sort" | "sort_by" | "reverse" | "dedup" => format!(
"`{name}` is not supported on vectors by the C backend yet; write the loop \
(for example an insertion sort over indices)"
),
_ => format!(
"no function or method named `{name}` exists for the C backend; check the \
spelling, define it as a `fn`, or declare it in an `extern \"C\"` block"
),
}
}
pub fn is_bound_variable(source: &str, name: &str) -> bool {
let word = |rest: &str| {
rest.strip_prefix(name)
.map(|after| !after.starts_with(|c: char| c.is_alphanumeric() || c == '_'))
.unwrap_or(false)
};
source.match_indices("let ").any(|(i, _)| {
let rest = source[i + 4..].trim_start();
let rest = rest
.strip_prefix("mut ")
.map(str::trim_start)
.unwrap_or(rest);
word(rest)
}) || source.match_indices(name).any(|(i, _)| {
let before_ok = i == 0 || {
let b = source.as_bytes()[i - 1];
b == b'(' || b == b',' || b == b' '
};
before_ok
&& word(&source[i..])
&& source[i + name.len()..].trim_start().starts_with(':')
&& !source[i + name.len()..].trim_start().starts_with("::")
})
}
pub fn function_value_hint(name: &str) -> String {
format!(
"`{name}` holds a function value; calling a function stored in a variable, field or \
collection is not supported by the C backend yet. Call the function by its own \
name, or branch with `if`/`match` and call each function directly"
)
}
pub fn locate_call(source: &str, name: &str) -> Option<(u32, u32)> {
let bytes = source.as_bytes();
let mut from = 0;
while let Some(rel) = source[from..].find(name) {
let start = from + rel;
let end = start + name.len();
from = end;
let before_ok = start == 0 || {
let b = bytes[start - 1];
!(b.is_ascii_alphanumeric() || b == b'_')
};
let after = source[end..].trim_start();
let after_ok = after.starts_with('(') || after.starts_with("::<");
let is_def = source[..start].trim_end().ends_with("fn");
let in_comment = source[..start]
.rfind('\n')
.map_or(&source[..start], |nl| &source[nl..start])
.contains("//");
if before_ok && after_ok && !is_def && !in_comment {
return Some((start as u32, end as u32));
}
}
None
}
pub fn undeclared_temporary(body: &str) -> Option<String> {
let mut declared = BTreeSet::new();
let (signature, body) = body.split_once('{').unwrap_or(("", body));
for word in signature.split(|c: char| !(c == '_' || c.is_ascii_alphanumeric())) {
if is_temp(word) {
declared.insert(word.to_string());
}
}
for line in body.lines() {
let t = line.trim();
if !t.ends_with(';') || t.starts_with("goto") || t.starts_with("return") {
continue;
}
let lhs = match t.split_once('=') {
Some((lhs, _)) => lhs,
None => t.trim_end_matches(';'),
};
let words: Vec<&str> = lhs
.split(|c: char| c.is_whitespace() || c == '*' || c == '(' || c == ')' || c == '[')
.filter(|w| !w.is_empty())
.collect();
if words.len() >= 2 {
for w in &words[1..] {
if is_temp(w) {
declared.insert((*w).to_string());
break;
}
}
}
}
let bytes = body.as_bytes();
let mut i = 0;
let mut in_str = false;
while i < bytes.len() {
let c = bytes[i];
if in_str {
if c == b'\\' {
i += 2;
continue;
}
if c == b'"' {
in_str = false;
}
i += 1;
continue;
}
if c == b'"' {
in_str = true;
i += 1;
continue;
}
if c == b'_' || c.is_ascii_alphabetic() {
let start = i;
while i < bytes.len() && (bytes[i] == b'_' || bytes[i].is_ascii_alphanumeric()) {
i += 1;
}
let member = start > 0 && (bytes[start - 1] == b'.' || bytes[start - 1] == b'>');
let word = &body[start..i];
if !member && is_temp(word) && !declared.contains(word) {
return Some(word.to_string());
}
continue;
}
if c.is_ascii_digit() {
while i < bytes.len() && (bytes[i] == b'_' || bytes[i].is_ascii_alphanumeric()) {
i += 1;
}
continue;
}
i += 1;
}
None
}
fn is_temp(word: &str) -> bool {
word.len() > 1 && word.starts_with('_') && word[1..].bytes().all(|b| b.is_ascii_digit())
}
#[cfg(test)]
mod tests {
#[test]
fn undeclared_temporary_finds_a_read_of_a_missing_local() {
let body = "static void f(int64_t _9) {\n int64_t _1;\n int64_t _2;\n\n _1 = _9;\n _2 = _6;\n return;\n}\n";
assert_eq!(super::undeclared_temporary(body).as_deref(), Some("_6"));
}
#[test]
fn undeclared_temporary_accepts_declared_and_ignores_members_and_strings() {
let body = "static void g(void) {\n BuildString _3;\n int8_t* _4;\n int64_t _5[4];\n int32_t (*_6)(int32_t);\n int64_t _7 = n;\n\n _4 = _3.ptr;\n _5[0] = _7;\n h._9 = 1;\n p->_10 = 2;\n printf(\"_11 %d\", _7);\n _6 = f;\n}\n";
assert_eq!(super::undeclared_temporary(body), None);
}
use super::*;
#[test]
fn reports_a_call_with_no_declaration() {
let c = "static int helper(int x) { return x; }\n// End BuildLang Runtime\n\
int32_t main(void) { int32_t a = helper(1); _3 = iter(v); return 0; }";
assert_eq!(undeclared_calls(c), vec!["iter".to_string()]);
}
#[test]
fn accepts_runtime_stdlib_macros_and_function_pointers() {
let c = "#define BL_CHECK(x) (x)\nstatic void* build_alloc(size_t n);\n\
// End BuildLang Runtime\nint32_t f(int32_t (*cb)(int32_t)) { return cb(1); }\n\
int32_t main(void) { printf(\"%d\", 1); void* p = build_alloc(4); \
if (BL_CHECK(1)) { return f(0); } return (int32_t)sqrt(4.0); }";
assert!(undeclared_calls(c).is_empty(), "{:?}", undeclared_calls(c));
}
#[test]
fn accepts_names_built_by_token_pasting() {
let c = "#define DEF(S, T) static T bl_iadd_##S(T a, T b) { return a + b; }
DEF(i32, int32_t)
// End BuildLang Runtime
int32_t main(void) { return bl_iadd_i32(1, 2); }";
assert!(undeclared_calls(c).is_empty(), "{:?}", undeclared_calls(c));
}
#[test]
fn ignores_calls_inside_strings_and_comments() {
let c = "// End BuildLang Runtime\nint32_t main(void) { /* iter(x) */ \
printf(\"parse(x)\"); return 0; }";
assert!(undeclared_calls(c).is_empty());
}
#[test]
fn a_plain_variable_does_not_hide_an_undeclared_call() {
let c = "typedef struct { size_t len; } S;\n// End BuildLang Runtime\n\
int32_t main(void) { return len(x); }";
assert_eq!(undeclared_calls(c), vec!["len".to_string()]);
}
#[test]
fn detects_function_values_bound_with_let_or_as_parameters() {
assert!(is_bound_variable("let loader = read_file;", "loader"));
assert!(is_bound_variable("let mut f = g;", "f"));
assert!(is_bound_variable("fn apply(cb: fn(i32) -> i32) {}", "cb"));
assert!(!is_bound_variable("let loaders = x;", "loader"));
assert!(!is_bound_variable("fn iter() {}", "iter"));
}
#[test]
fn locates_method_calls_and_skips_definitions() {
let src = "fn iter() {}\nfn main() { // v.iter()\n for x in v.iter() {} }";
let (start, _) = locate_call(src, "iter").unwrap();
assert_eq!(&src[start as usize..start as usize + 4], "iter");
assert!(src[..start as usize].ends_with("v."));
}
}