|
| 1 | +#ifndef CBM_LSP_RUBY_LSP_H |
| 2 | +#define CBM_LSP_RUBY_LSP_H |
| 3 | + |
| 4 | +#include "type_rep.h" |
| 5 | +#include "scope.h" |
| 6 | +#include "type_registry.h" |
| 7 | +#include "../cbm.h" |
| 8 | + |
| 9 | +/* Ruby mixin kinds. Lookup order for an instance method on class C: |
| 10 | + * prepended modules (last prepended first) → C's own methods → |
| 11 | + * included modules (last included first) → superclass chain. |
| 12 | + * `extend` adds a module's instance methods to C's singleton (class-side). */ |
| 13 | +typedef enum { |
| 14 | + RUBY_MIXIN_INCLUDE = 0, |
| 15 | + RUBY_MIXIN_PREPEND, |
| 16 | + RUBY_MIXIN_EXTEND, |
| 17 | +} RubyMixinKind; |
| 18 | + |
| 19 | +/* One class/module discovered in the file (or, cross-file, in the project). |
| 20 | + * `path` is the dotted constant path used ONLY for constant lookup ("Admin.User" |
| 21 | + * for both `module Admin; class User` and `class Admin::User`). `qn` is the |
| 22 | + * graph QN built exactly the way the structural extractor builds it: each |
| 23 | + * declaration's raw name text appended to the enclosing class QN (or the |
| 24 | + * module QN at top level), so a compact `class Admin::User` yields |
| 25 | + * "<module_qn>.Admin::User" while the lexically nested form yields |
| 26 | + * "<module_qn>.Admin.User". Emitted rows must carry the extractor spelling |
| 27 | + * or the pipeline's exact-name join silently drops them. `superclass_qn` is |
| 28 | + * resolved against the class table + stdlib after collection; NULL when |
| 29 | + * unknown. */ |
| 30 | +typedef struct { |
| 31 | + const char *path; |
| 32 | + const char *qn; |
| 33 | + const char *superclass_ref; /* raw dotted superclass spelling, or NULL */ |
| 34 | + const char *superclass_qn; /* resolved QN (table/stdlib), or NULL */ |
| 35 | + bool is_module; /* `module` (mixin source) vs `class` */ |
| 36 | + /* Method-lookup visit marks: the stamp of the last walk that searched |
| 37 | + * this class on the instance / singleton side (ruby_walk_seen). */ |
| 38 | + unsigned int seen_instance; |
| 39 | + unsigned int seen_singleton; |
| 40 | +} RubyClassInfo; |
| 41 | + |
| 42 | +/* One `include`/`prepend`/`extend` record: owner class QN → module ref. */ |
| 43 | +typedef struct { |
| 44 | + const char *owner_qn; /* class/module whose body held the call */ |
| 45 | + const char *module_ref; /* raw dotted module spelling */ |
| 46 | + const char *module_qn; /* resolved QN, or NULL */ |
| 47 | + RubyMixinKind kind; |
| 48 | +} RubyMixinInfo; |
| 49 | + |
| 50 | +/* One inferred instance-variable type: (class QN, @name) → type QN. |
| 51 | + * `conflicted` latches when two assignments disagree — lookup then returns |
| 52 | + * nothing (zero-edge guarantee beats a coin-flip type). */ |
| 53 | +typedef struct { |
| 54 | + const char *class_qn; |
| 55 | + const char *ivar_name; /* including the leading '@' */ |
| 56 | + const char *type_qn; |
| 57 | + bool conflicted; |
| 58 | +} RubyIvarInfo; |
| 59 | + |
| 60 | +/* RubyLSPContext — per-file state for Ruby type-aware call resolution. |
| 61 | + * Mirrors PerlLSPContext / PHPLSPContext structure. |
| 62 | + * |
| 63 | + * Ruby specifics that shape this context: |
| 64 | + * - Classes/modules nest lexically (`module A; class B`), and constants |
| 65 | + * resolve through the lexical nesting outward (A::B sees A's constants |
| 66 | + * before top-level ones). `nesting` tracks the current dotted path. |
| 67 | + * - Inheritance is `class C < Base`; mixins arrive via include/prepend/ |
| 68 | + * extend and participate in method lookup (see RubyMixinKind). |
| 69 | + * - `Foo.new` constructs a Foo and dispatches to Foo#initialize. |
| 70 | + * - `def m` defines an instance method; `def self.m` a singleton (class) |
| 71 | + * method. Singleton methods are registered under the receiver key |
| 72 | + * "<class_qn>.self" — constants cannot be lowercase, so the suffix can |
| 73 | + * never collide with a real nested-constant path. */ |
| 74 | +typedef struct { |
| 75 | + CBMArena *arena; |
| 76 | + const char *source; |
| 77 | + int source_len; |
| 78 | + const CBMTypeRegistry *registry; |
| 79 | + /* Mutable alias of `registry`, set only while PASS 1 populates method |
| 80 | + * tables (mirrors perl_lsp's mutable-reg build phase). NULL afterwards. */ |
| 81 | + CBMTypeRegistry *build_reg; |
| 82 | + CBMScope *current_scope; |
| 83 | + |
| 84 | + /* Lexical nesting: one frame per enclosing class/module declaration, |
| 85 | + * frames joined by '.', each frame keeping its raw source spelling ("" at |
| 86 | + * top level, "M.A::B" inside `module M; class A::B`). Ruby's constant |
| 87 | + * lookup walks the frames outward — inside `class A::B` the nesting is |
| 88 | + * [A::B] only, so A's constants are NOT probed. */ |
| 89 | + const char *nesting; |
| 90 | + |
| 91 | + /* Class/module table (pass 1). */ |
| 92 | + RubyClassInfo *classes; |
| 93 | + int class_count; |
| 94 | + int class_cap; |
| 95 | + |
| 96 | + /* Mixin table (pass 1). */ |
| 97 | + RubyMixinInfo *mixins; |
| 98 | + int mixin_count; |
| 99 | + int mixin_cap; |
| 100 | + |
| 101 | + /* Instance-variable type table (pass 1). */ |
| 102 | + RubyIvarInfo *ivars; |
| 103 | + int ivar_count; |
| 104 | + int ivar_cap; |
| 105 | + |
| 106 | + /* Cross-file import map (require/require_relative resolved by the |
| 107 | + * pipeline). Unused per-file: Ruby constants are file-global. */ |
| 108 | + const char **import_names; |
| 109 | + const char **import_qns; |
| 110 | + int import_count; |
| 111 | + |
| 112 | + /* Current enclosing context during the resolution walk. */ |
| 113 | + const char *enclosing_class_qn; /* class QN, or NULL at top level */ |
| 114 | + const char *enclosing_func_qn; /* enclosing method QN, or NULL */ |
| 115 | + bool in_singleton_method; /* `def self.m` / class << self scope */ |
| 116 | + /* Inside a block that rebinds `self` (instance_eval/exec, class_eval/ |
| 117 | + * exec, module_eval/exec, define_method, Class.new/Module.new): the |
| 118 | + * receiver of bare and `self.` calls is NOT the enclosing class, and |
| 119 | + * the resolver cannot know what it is — so they emit nothing. */ |
| 120 | + bool self_unknown; |
| 121 | + /* Number of enclosing blocks (`{ }`, `do end`, `-> { }`) since the |
| 122 | + * nearest class/module body. A `def` met here is NOT a method of the |
| 123 | + * enclosing class — ActiveSupport's `class_methods do … end`, |
| 124 | + * `included do … end`, RSpec groups — so PASS 1 registers nothing for |
| 125 | + * it and collects no mixins from it. */ |
| 126 | + int block_depth; |
| 127 | + const char *module_qn; |
| 128 | + |
| 129 | + /* Output: resolved calls accumulate here. */ |
| 130 | + CBMResolvedCallArray *resolved_calls; |
| 131 | + |
| 132 | + /* Recursion guards. */ |
| 133 | + int eval_depth; |
| 134 | + int walk_depth; |
| 135 | + |
| 136 | + /* Stamp of the current method-lookup walk; each walk takes a fresh one |
| 137 | + * so marks left by earlier walks never match. */ |
| 138 | + unsigned int lookup_stamp; |
| 139 | + |
| 140 | + /* Debug mode (CBM_LSP_DEBUG env). */ |
| 141 | + bool debug; |
| 142 | +} RubyLSPContext; |
| 143 | + |
| 144 | +/* Initialize a RubyLSPContext for processing one file. */ |
| 145 | +void ruby_lsp_init(RubyLSPContext *ctx, CBMArena *arena, const char *source, int source_len, |
| 146 | + const CBMTypeRegistry *registry, const char *module_qn, |
| 147 | + CBMResolvedCallArray *out); |
| 148 | + |
| 149 | +/* Resolve a dotted constant path (e.g. "Foo", "A.B") against the class |
| 150 | + * table using the current lexical nesting, then the stdlib. Returns the |
| 151 | + * resolved QN or NULL. */ |
| 152 | +const char *ruby_resolve_constant(RubyLSPContext *ctx, const char *path); |
| 153 | + |
| 154 | +/* Look up an instance method on a class, walking prepends → own → includes → |
| 155 | + * superclass chain. Returns the resolved CBMRegisteredFunc or NULL. */ |
| 156 | +const CBMRegisteredFunc *ruby_lookup_instance_method(RubyLSPContext *ctx, const char *class_qn, |
| 157 | + const char *method_name); |
| 158 | + |
| 159 | +/* Look up a singleton (class-side) method: own singleton methods → |
| 160 | + * `extend`ed modules' instance methods → superclass singleton chain. */ |
| 161 | +const CBMRegisteredFunc *ruby_lookup_singleton_method(RubyLSPContext *ctx, const char *class_qn, |
| 162 | + const char *method_name); |
| 163 | + |
| 164 | +/* Evaluate a Ruby expression's type. May return CBM_TYPE_UNKNOWN. */ |
| 165 | +const CBMType *ruby_eval_expr_type(RubyLSPContext *ctx, TSNode node); |
| 166 | + |
| 167 | +/* Entry point: build registry from file defs + stdlib, then run resolution. |
| 168 | + * Called from cbm_extract_file() via the language dispatch in cbm.c. */ |
| 169 | +void cbm_run_ruby_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len, |
| 170 | + TSNode root); |
| 171 | + |
| 172 | +/* Register Ruby core stdlib types/methods + a curated Rails surface |
| 173 | + * (ActiveRecord/ActiveSupport/ActionController) into a registry. */ |
| 174 | +void cbm_ruby_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena); |
| 175 | + |
| 176 | +#endif /* CBM_LSP_RUBY_LSP_H */ |
0 commit comments