Skip to content

Commit 830f0cc

Browse files
committed
fix(pipeline): resolve PSR-4 imports to exact class files
Preserve import kinds and resolve class imports through Composer prefixes to the existing target class file, including autoload-dev mappings. Refs #1186 Signed-off-by: Martin Vogel <[email protected]>
1 parent 96c3f41 commit 830f0cc

4 files changed

Lines changed: 361 additions & 13 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,20 @@ typedef struct {
304304
// guard's unique-name exemption. Default false.
305305
} CBMCall;
306306

307+
// What an import statement names, when the syntax says so. Only PHP's
308+
// `use function` / `use const` set a non-default kind today: they name a
309+
// namespace MEMBER, so they must never be mapped to a class file (#1186).
310+
typedef enum {
311+
CBM_IMPORT_KIND_DEFAULT = 0, // module / class / namespace (language default)
312+
CBM_IMPORT_KIND_FUNCTION, // PHP `use function A\b`
313+
CBM_IMPORT_KIND_CONST, // PHP `use const A\B`
314+
} CBMImportKind;
315+
307316
typedef struct {
308317
const char *local_name; // local alias or name
309318
const char *module_path; // resolved module path / QN
310319
bool is_default; // ES default import (`import X from "Y"`), JS/TS only (#1916)
320+
CBMImportKind kind; // CBM_IMPORT_KIND_DEFAULT unless the syntax names a member kind
311321
} CBMImport;
312322

313323
typedef enum {

‎internal/cbm/extract_imports.c‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,29 @@ static void parse_wolfram_imports(CBMExtractCtx *ctx) {
12051205
// require/include forms remain `expression_statement`s and are still handled by
12061206
// the text fallback. Take the first qualified_name/name descendant of each
12071207
// clause as the module path.
1208-
static void emit_php_use_clause(CBMExtractCtx *ctx, TSNode clause, const char *group_prefix) {
1208+
// `use function` / `use const` carry the keyword as an anonymous direct child
1209+
// of the declaration (flat form) or of the clause (grouped form
1210+
// `use A\{function f, const C, K}`). DEFAULT when neither is present.
1211+
static CBMImportKind php_use_kind(TSNode node) {
1212+
uint32_t n = ts_node_child_count(node);
1213+
for (uint32_t i = 0; i < n; i++) {
1214+
TSNode c = ts_node_child(node, i);
1215+
if (ts_node_is_named(c)) {
1216+
continue;
1217+
}
1218+
const char *k = ts_node_type(c);
1219+
if (strcmp(k, "function") == 0) {
1220+
return CBM_IMPORT_KIND_FUNCTION;
1221+
}
1222+
if (strcmp(k, "const") == 0) {
1223+
return CBM_IMPORT_KIND_CONST;
1224+
}
1225+
}
1226+
return CBM_IMPORT_KIND_DEFAULT;
1227+
}
1228+
1229+
static void emit_php_use_clause(CBMExtractCtx *ctx, TSNode clause, const char *group_prefix,
1230+
CBMImportKind decl_kind) {
12091231
CBMArena *a = ctx->arena;
12101232
// The path node is the qualified_name / namespace_name / name child.
12111233
TSNode path_node = clause;
@@ -1230,12 +1252,14 @@ static void emit_php_use_clause(CBMExtractCtx *ctx, TSNode clause, const char *g
12301252
TSNode alias = ts_node_child_by_field_name(clause, TS_FIELD("alias"));
12311253
const char *local =
12321254
!ts_node_is_null(alias) ? cbm_node_text(a, alias, ctx->source) : path_last(a, path);
1233-
CBMImport imp = {.local_name = local, .module_path = path};
1255+
CBMImportKind kind = decl_kind != CBM_IMPORT_KIND_DEFAULT ? decl_kind : php_use_kind(clause);
1256+
CBMImport imp = {.local_name = local, .module_path = path, .kind = kind};
12341257
cbm_imports_push(&ctx->result->imports, a, imp);
12351258
}
12361259

12371260
static void emit_php_use_decl(CBMExtractCtx *ctx, TSNode decl) {
12381261
CBMArena *a = ctx->arena;
1262+
const CBMImportKind decl_kind = php_use_kind(decl);
12391263
// Grouped form: namespace_use_group with a leading prefix qualified_name.
12401264
TSNode group = decl;
12411265
if (find_first_descendant_of(decl, "namespace_use_group", &group)) {
@@ -1257,7 +1281,7 @@ static void emit_php_use_decl(CBMExtractCtx *ctx, TSNode decl) {
12571281
const char *ck = ts_node_type(clause);
12581282
if (strcmp(ck, "namespace_use_group_clause") == 0 ||
12591283
strcmp(ck, "namespace_use_clause") == 0) {
1260-
emit_php_use_clause(ctx, clause, prefix);
1284+
emit_php_use_clause(ctx, clause, prefix, decl_kind);
12611285
}
12621286
}
12631287
return;
@@ -1268,13 +1292,13 @@ static void emit_php_use_decl(CBMExtractCtx *ctx, TSNode decl) {
12681292
for (uint32_t i = 0; i < dc; i++) {
12691293
TSNode clause = ts_node_named_child(decl, i);
12701294
if (strcmp(ts_node_type(clause), "namespace_use_clause") == 0) {
1271-
emit_php_use_clause(ctx, clause, NULL);
1295+
emit_php_use_clause(ctx, clause, NULL, decl_kind);
12721296
any = true;
12731297
}
12741298
}
12751299
if (!any) {
12761300
// Some grammar versions inline the path directly under the declaration.
1277-
emit_php_use_clause(ctx, decl, NULL);
1301+
emit_php_use_clause(ctx, decl, NULL, decl_kind);
12781302
}
12791303
}
12801304

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,12 @@ static void parse_pyproject_toml(const char *source, int source_len, const char
440440
}
441441
}
442442

443-
/* Extract PSR-4 autoload entries from composer.json root. */
444-
static void extract_psr4(yyjson_val *root, const char *dir, cbm_pkg_entries_t *entries) {
445-
yyjson_val *autoload = yyjson_obj_get(root, "autoload");
443+
/* Extract PSR-4 entries from one composer.json autoload section ("autoload"
444+
* or "autoload-dev": composer registers both, so test namespaces such as
445+
* Tests\ map to their directory as well). */
446+
static void extract_psr4(yyjson_val *root, const char *section, const char *dir,
447+
cbm_pkg_entries_t *entries) {
448+
yyjson_val *autoload = yyjson_obj_get(root, section);
446449
if (!yyjson_is_obj(autoload)) {
447450
return;
448451
}
@@ -459,6 +462,12 @@ static void extract_psr4(yyjson_val *root, const char *dir, cbm_pkg_entries_t *e
459462
}
460463
const char *ns_prefix = yyjson_get_str(key);
461464
const char *ns_dir = yyjson_get_str(val);
465+
while (ns_dir[0] == '.' && ns_dir[SKIP_ONE] == '/') {
466+
ns_dir += PAIR_LEN; /* "./src/" names src/ */
467+
}
468+
if (strcmp(ns_dir, ".") == 0) {
469+
ns_dir = ""; /* "." names the manifest's own directory */
470+
}
462471
char ns_entry[PKGMAP_PATH_BUF];
463472
if (dir[0]) {
464473
snprintf(ns_entry, sizeof(ns_entry), "%s/%s", dir, ns_dir);
@@ -497,7 +506,8 @@ static void parse_composer_json(const char *source, int source_len, const char *
497506
}
498507
}
499508

500-
extract_psr4(root, dir, entries);
509+
extract_psr4(root, "autoload", dir, entries);
510+
extract_psr4(root, "autoload-dev", dir, entries);
501511

502512
free(dir);
503513
yyjson_doc_free(doc);
@@ -987,6 +997,12 @@ bool cbm_pkgmap_try_parse(const char *basename, const char *rel_path, const char
987997

988998
/* ── Merge: per-worker entries → hash table ────────────────────── */
989999

1000+
/* A composer.json `autoload.psr-4` key: a namespace prefix ending in '\\'. */
1001+
static bool pkgmap_is_psr4_prefix(const char *pkg_name) {
1002+
size_t n = pkg_name ? strlen(pkg_name) : 0;
1003+
return n > 0 && pkg_name[n - SKIP_ONE] == '\\';
1004+
}
1005+
9901006
CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_count,
9911007
const char *project_name) {
9921008
/* Count total entries */
@@ -1004,8 +1020,14 @@ CBMHashTable *cbm_pkgmap_build(cbm_pkg_entries_t *worker_entries, int worker_cou
10041020
for (int w = 0; w < worker_count; w++) {
10051021
cbm_pkg_entries_t *we = &worker_entries[w];
10061022
for (int i = 0; i < we->count; i++) {
1007-
/* Convert entry_rel to QN: project.dir.parts */
1008-
char *qn = cbm_pipeline_fqn_module(project_name, we->items[i].entry_rel);
1023+
/* Convert entry_rel to QN: project.dir.parts. A PSR-4 namespace
1024+
* prefix (key ends in '\\') keeps its repo-relative DIRECTORY
1025+
* instead: its readers map `Prefix\Sub\Class` onto a file path
1026+
* (<dir>/Sub/Class.php), which a dotted QN cannot express (#1186). */
1027+
const char *entry_rel = we->items[i].entry_rel;
1028+
char *qn = pkgmap_is_psr4_prefix(we->items[i].pkg_name)
1029+
? cbm_strndup(entry_rel, strlen(entry_rel))
1030+
: cbm_pipeline_fqn_module(project_name, entry_rel);
10091031
if (!qn) {
10101032
continue;
10111033
}
@@ -1355,7 +1377,8 @@ static char *resolve_dot_prefix(CBMHashTable *map, const char *module_path,
13551377
}
13561378

13571379
/* Try backslash-based prefix matching (PHP PSR-4: App\\Controllers\\Foo).
1358-
* Returns heap QN or NULL. */
1380+
* The map value of a PSR-4 prefix is its repo-relative directory (see
1381+
* cbm_pkgmap_build). Returns heap QN or NULL. */
13591382
static char *resolve_backslash_prefix(CBMHashTable *map, const char *module_path,
13601383
const char *project_name) {
13611384
char *buf = strdup(module_path);
@@ -1375,7 +1398,8 @@ static char *resolve_backslash_prefix(CBMHashTable *map, const char *module_path
13751398
}
13761399
const char *subpath = module_path + (size_t)(bs - buf) + SKIP_ONE;
13771400
char path_result[PKGMAP_PATH_BUF];
1378-
snprintf(path_result, sizeof(path_result), "%s/%s", base_dir, subpath);
1401+
snprintf(path_result, sizeof(path_result), "%s%s%s", base_dir, base_dir[0] ? "/" : "",
1402+
subpath);
13791403
for (char *c = path_result; *c; c++) {
13801404
if (*c == '\\') {
13811405
*c = '/';
@@ -2025,6 +2049,91 @@ static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx
20252049
return found;
20262050
}
20272051

2052+
/* ── PHP PSR-4 class imports (#1186) ──────────────────────────────── */
2053+
2054+
typedef enum {
2055+
PSR4_NOT_APPLICABLE = 0, /* no composer psr-4 prefix covers the import */
2056+
PSR4_RESOLVED, /* the class file exists: *out is its File node */
2057+
PSR4_UNRESOLVED, /* a prefix covers it but no class file exists */
2058+
} psr4_outcome_t;
2059+
2060+
/* The File node at exactly `rel_path`, or NULL. Looked up through the name
2061+
* index (File nodes are named by basename) and matched on the full path, so
2062+
* a same-named file in another directory never qualifies. */
2063+
static const cbm_gbuf_node_t *psr4_file_node(const cbm_pipeline_ctx_t *ctx, const char *rel_path) {
2064+
const cbm_gbuf_node_t **hits = NULL;
2065+
int hit_count = 0;
2066+
if (cbm_gbuf_find_by_name(ctx->gbuf, path_leaf(rel_path), &hits, &hit_count) != 0 || !hits) {
2067+
return NULL;
2068+
}
2069+
for (int i = 0; i < hit_count; i++) {
2070+
const cbm_gbuf_node_t *n = hits[i];
2071+
if (n && n->label && strcmp(n->label, "File") == 0 && n->file_path &&
2072+
strcmp(n->file_path, rel_path) == 0) {
2073+
return n;
2074+
}
2075+
}
2076+
return NULL;
2077+
}
2078+
2079+
/* Resolve a PHP class import `use A\B\C;` the way composer's PSR-4 autoloader
2080+
* does: for every autoload.psr-4 prefix covering the name, longest first, the
2081+
* class lives in exactly <prefix-dir>/<rest>.php with sub-namespaces as
2082+
* subdirectories; the first existing file wins. When a prefix covers the name
2083+
* but no such file exists the import is UNRESOLVED: it must never fall through
2084+
* to the namespace bucket, which binds it to whichever file of the namespace
2085+
* came first. `use function` / `use const` name namespace members, not class
2086+
* files, so they are not applicable. */
2087+
static psr4_outcome_t resolve_php_psr4_class(const cbm_pipeline_ctx_t *ctx,
2088+
const char *source_file_qn, const CBMImport *imp,
2089+
const cbm_gbuf_node_t **out) {
2090+
*out = NULL;
2091+
CBMHashTable *pkgmap = cbm_pipeline_get_pkgmap();
2092+
if (!pkgmap || imp->kind != CBM_IMPORT_KIND_DEFAULT) {
2093+
return PSR4_NOT_APPLICABLE;
2094+
}
2095+
const char *name = imp->module_path;
2096+
if (name[0] == '\\') {
2097+
name++; /* fully qualified `use \App\X;` */
2098+
}
2099+
char key[PKGMAP_PATH_BUF];
2100+
size_t name_len = strlen(name);
2101+
if (name_len == 0 || name_len >= sizeof(key) || !strchr(name, '\\')) {
2102+
return PSR4_NOT_APPLICABLE;
2103+
}
2104+
bool covered = false;
2105+
/* Right to left: each '\' ends a candidate prefix, longest first. */
2106+
for (size_t cut = name_len - SKIP_ONE; cut > 0; cut--) {
2107+
if (name[cut - SKIP_ONE] != '\\') {
2108+
continue;
2109+
}
2110+
memcpy(key, name, cut);
2111+
key[cut] = '\0';
2112+
const char *dir = (const char *)cbm_ht_get(pkgmap, key);
2113+
if (!dir) {
2114+
continue;
2115+
}
2116+
covered = true;
2117+
char rel[PKGMAP_PATH_BUF];
2118+
int w = snprintf(rel, sizeof(rel), "%s%s%s.php", dir, dir[0] ? "/" : "", name + cut);
2119+
if (w <= 0 || (size_t)w >= sizeof(rel)) {
2120+
continue;
2121+
}
2122+
for (char *c = rel; *c; c++) {
2123+
if (*c == '\\') {
2124+
*c = '/';
2125+
}
2126+
}
2127+
const cbm_gbuf_node_t *file = psr4_file_node(ctx, rel);
2128+
if (file && (!source_file_qn || !file->qualified_name ||
2129+
strcmp(file->qualified_name, source_file_qn) != 0)) {
2130+
*out = file;
2131+
return PSR4_RESOLVED;
2132+
}
2133+
}
2134+
return covered ? PSR4_UNRESOLVED : PSR4_NOT_APPLICABLE;
2135+
}
2136+
20282137
const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx,
20292138
const char *source_rel,
20302139
const char *source_file_qn,
@@ -2042,6 +2151,18 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
20422151
return header_target;
20432152
}
20442153

2154+
/* PHP class imports covered by a composer psr-4 prefix name exactly one
2155+
* file; when it is absent the import stays unresolved (#1186). */
2156+
const cbm_gbuf_node_t *psr4_target = NULL;
2157+
switch (resolve_php_psr4_class(ctx, source_file_qn, imp, &psr4_target)) {
2158+
case PSR4_RESOLVED:
2159+
return psr4_target;
2160+
case PSR4_UNRESOLVED:
2161+
return NULL;
2162+
case PSR4_NOT_APPLICABLE:
2163+
break;
2164+
}
2165+
20452166
/* Strategy 1: module-path resolution → existing node (Python/TS/Go).
20462167
* No label filter here: directory-module languages (Go/Java packages)
20472168
* legitimately resolve straight to a Folder node -- that's the intended,

0 commit comments

Comments
 (0)