#include // std::from_chars (elixirCallArity); Apple clang does not get it transitively #pragma once #if !defined( RIPWIRE_INGEST_TU ) #error "ingest_elixir.h is a section of ingest.cpp; include it only there" #endif namespace rw { namespace { // Included by ingest.cpp after the shared AST helpers. // Elixir keywords are identifiers: tags.scm tells def f(x) from ordinary(f(x)) only through its // string predicates (#355). The text gates that predicates cannot express stay local to this grammar. /// Return an identifier call target as a view into src, or an empty view for null or remote targets. std::string_view elixirTarget( TSNode node, std::string_view src ) noexcept { if( ts_node_is_null( node ) ) { return {}; } return fieldIdentifierText( node, NodeField::Target, src ); } /// The definition keywords (def, defp, defmacro, ...) are DATA: the #any-of? list on the @_keyword capture of /// queries/elixir/tags.scm's keyword-headed @definition.function patterns, which the tags pass applies itself (#355). /// A tags match is a keyword-headed definition when it carries @_keyword. The tree walks below that must recognise a /// definition head OUTSIDE a match (an enclosing `def` while scoping a binding, a `def` call that is not a reference) /// read the same list here, filled by ElixirContext::prepare from the file's compiled query, instead of a C++ copy. /// The metadata attributes (@doc, @spec, ...) are the same kind of data: the #not-any-of? list on @_attribute of the /// @definition.attribute pattern, read back for the references INSIDE such an attribute, which are not call sites. struct ElixirKeywords { std::vector function; // views into the TSQuery's string table, which outlives every file std::vector metadata; // the same, for the metadata attribute names /// Return whether target introduces a function, macro, guard, or delegate definition. bool isFunction( std::string_view target ) const noexcept { return std::find( function.begin(), function.end(), target ) != function.end(); } }; /// Return whether target introduces a statically named module or protocol, excluding defimpl. bool elixirModuleKeyword( std::string_view target ) noexcept { return target == "defmodule" || target == "defprotocol"; } /// Find the direct arguments child of node; return a null node when node or its arguments are absent. TSNode elixirArguments( TSNode node ) noexcept { if( ts_node_is_null( node ) ) { return {}; } // O(children): tree-sitter-elixir accepts comments between a call's head and its do-block and splices // them into the call node itself — 16 000 of them measured 82x under elixirBody's twin of this scan // (test/childwalkscalecheck.sh, arm B30, attributed by `sample`; the rule is on src/infra/tschildren.h) return firstChildOfKind( node, /*namedOnly=*/true, { "arguments" } ); } /// Return the first direct call argument, or a null node for an absent or empty argument list. TSNode elixirFirstArgument( TSNode node ) noexcept { const TSNode args = elixirArguments( node ); for( std::uint32_t i = 0; !ts_node_is_null( args ) && i < ts_node_named_child_count( args ); ++i ) { const TSNode child = ts_node_named_child( args, i ); if( std::strcmp( ts_node_type( child ), "comment" ) != 0 ) { return child; } } return {}; } // Both `do ... end` and `do: expression` carry a body, but neither has a body: field. /// Find the value of a `key:` entry in node's own keyword arguments (`def f(), do: 1`, `defimpl P, for: T`). /// key carries its trailing colon. Return a null node when node has no such keyword. TSNode elixirKeywordValue( TSNode node, std::string_view key, std::string_view src ) noexcept { const TSNode args = elixirArguments( node ); if( ts_node_is_null( args ) ) { return {}; } // O(children) at both levels: `def f(x), # … do: x` puts the comments in the arguments (81x at 16 000, // test/childwalkscalecheck.sh arm B29). Two cursors: the pair walk runs while the argument walk is open. TSNode value = {}; bool matched = false; ChildCursor argCursor( args ); ChildCursor pairCursor( args ); forEachNamedChild( args, argCursor.cur, [ & ]( TSNode arg ) { if( std::strcmp( ts_node_type( arg ), "keywords" ) != 0 ) { return true; } forEachNamedChild( arg, pairCursor.cur, [ & ]( TSNode pair ) { auto found = nodeTextOf( fieldChild( pair, NodeField::Key ), src ); while( !found.empty() && std::isspace( static_cast( found.back() ) ) ) { found.remove_suffix( 1 ); } if( found != key ) { return true; } value = fieldChild( pair, NodeField::Value ); matched = true; return false; } ); return !matched; } ); return value; } /// Find a definition's direct do-block or do-keyword value without adopting an ancestor's body. /// node must be non-null; src must contain its source span. Return a null node when no body exists. TSNode elixirBody( TSNode node, std::string_view src ) noexcept { // O(children) — arm B30 in test/childwalkscalecheck.sh; the note is on elixirArguments const TSNode block = firstChildOfKind( node, /*namedOnly=*/true, { "do_block" } ); return ts_node_is_null( block ) ? elixirKeywordValue( node, "do:", src ) : block; } /// Count syntactic parameters in an ordinary or guarded definition head, saturating at UINT16_MAX. /// Missing argument lists count as zero; this does not infer callable arities from default values. std::uint16_t elixirParams( TSNode node, std::string_view src ) noexcept { TSNode head = elixirFirstArgument( node ); if( !ts_node_is_null( head ) && std::strcmp( ts_node_type( head ), "binary_operator" ) == 0 && nodeFieldText( head, NodeField::Operator, src ) == "when" ) { head = fieldChild( head, NodeField::Left ); } if( !ts_node_is_null( head ) && std::strcmp( ts_node_type( head ), "binary_operator" ) == 0 ) { return 2; } if( !ts_node_is_null( head ) && std::strcmp( ts_node_type( head ), "unary_operator" ) == 0 ) { return 1; } const TSNode args = elixirArguments( head ); std::uint32_t count = 0; for( std::uint32_t i = 0; !ts_node_is_null( args ) && i < ts_node_named_child_count( args ); ++i ) { count += std::strcmp( ts_node_type( ts_node_named_child( args, i ) ), "comment" ) != 0 ? 1u : 0u; } return std::uint16_t( std::min( count, std::uint32_t( 65535 ) ) ); } std::string_view elixirAttribute( TSNode node, std::string_view src ) noexcept { if( ts_node_is_null( node ) || nodeFieldText( node, NodeField::Operator, src ) != "@" ) { return {}; } return elixirTarget( fieldChild( node, NodeField::Operand ), src ); } bool elixirTypedAttribute( std::string_view name ) noexcept { return name == "type" || name == "typep" || name == "opaque" || name == "callback" || name == "macrocallback"; } /// Return whether @name is compile-time metadata, never a runtime value: a typed attribute, or one of tags.scm's /// metadata names (ElixirKeywords::metadata, #355). bool elixirMetadataAttribute( std::string_view name, const ElixirKeywords& keywords ) noexcept { return elixirTypedAttribute( name ) || std::find( keywords.metadata.begin(), keywords.metadata.end(), name ) != keywords.metadata.end(); } bool elixirLiveHole( TSNode hole, TSNode site, std::string_view src ) noexcept; /// What one tags match says about its role node, as elixirKeepCapture reads it. struct ElixirCaptureRole { bool isDef = false; // the role is an @definition.* capture SymKind kind = SymKind::Other; // the role's symbol kind bool keywordHead = false; // the match carried @_keyword: tags.scm's predicates already decided it is a // keyword-headed definition (#any-of? on the keyword, #not-any-of? "unquote" // "when" on the name, #355) }; /// Decide whether a candidate definition or call capture represents supported executable Elixir syntax. /// role and name are non-null query captures into src. Reject quoted and dynamic syntax and metadata /// references; retain executable defaults and attributes while excluding declaration heads and patterns. /// capture: what the tags match says about the role node (see ElixirCaptureRole). bool elixirKeepCapture( TSNode role, TSNode name, ElixirCaptureRole capture, std::string_view src, const ElixirKeywords& keywords ) noexcept { const bool isDef = capture.isDef; const SymKind kind = capture.kind; // Module attributes (notably @spec/@type) are not runtime call sites. // The shared inert-region decision runs after this capture's other syntax filters, // so quoted definition heads and special forms never inflate declined=. for( TSNode parent = parentOf( role ); !ts_node_is_null( parent ); parent = parentOf( parent ) ) { if( elixirLiveHole( parent, role, src ) ) { break; } // outer quoted declarations do not constrain the evaluated argument if( std::strcmp( ts_node_type( parent ), "unary_operator" ) == 0 && nodeFieldText( parent, NodeField::Operator, src ) == "@" ) { if( elixirMetadataAttribute( elixirAttribute( parent, src ), keywords ) || ts_node_eq( fieldChild( parent, NodeField::Operand ), role ) ) { return false; } } } const auto target = elixirTarget( role, src ); if( isDef ) { if( const auto attribute = elixirAttribute( role, src ); !attribute.empty() ) { // A metadata attribute (@doc, @spec, ...) never reaches here: tags.scm's #not-any-of? on @_attribute // rejected its @definition.attribute match (#355). A typed one is the @definition.type pattern's. return kind == SymKind::Struct ? elixirTypedAttribute( attribute ) : !elixirTypedAttribute( attribute ); } if( target == "test" && std::strcmp( ts_node_type( name ), "string" ) == 0 ) { const auto title = nodeTextOf( name, src ); if( title.size() < 2 || title.front() != '"' || title.back() != '"' || title.starts_with( "\"\"\"" ) ) { return false; // only complete, ordinary string titles have a static display name here } // indexed on purpose: a string's children come from the external scanner, which owns every byte // between the quotes, so no comment token can be lexed into this list (src/infra/tschildren.h) for( std::uint32_t childId = 0; childId < ts_node_named_child_count( name ); ++childId ) { if( std::strcmp( ts_node_type( ts_node_named_child( name, childId ) ), "interpolation" ) == 0 ) { return false; } } return true; } // `defimpl` defines a real module (`Protocol.For`); ElixirContext supplies its full name. if( kind == SymKind::Other ) { return elixirModuleKeyword( target ) || target == "defimpl"; } return capture.keywordHead; // the keyword list and the unquote/when exclusion live in queries/elixir/tags.scm (#355) } const TSNode firstArg = elixirFirstArgument( role ); if( target == "test" && !ts_node_is_null( firstArg ) && std::strcmp( ts_node_type( firstArg ), "string" ) == 0 && !ts_node_is_null( elixirBody( role, src ) ) ) { return false; // the test declaration itself is not a call } const TSNode callTarget = fieldChild( role, NodeField::Target ); if( !ts_node_is_null( callTarget ) && std::strcmp( ts_node_type( callTarget ), "dot" ) == 0 ) { const TSNode receiver = fieldChild( callTarget, NodeField::Left ); if( ts_node_is_null( receiver ) || ( std::strcmp( ts_node_type( receiver ), "alias" ) != 0 && std::strcmp( ts_node_type( receiver ), "atom" ) != 0 && std::strcmp( ts_node_type( receiver ), "dot" ) != 0 && nodeTextOf( receiver, src ) != "__MODULE__" ) ) { return false; // runtime receiver / anonymous function dispatch cannot name a module } } constexpr std::string_view special[] = { "defimpl", "defstruct", "defexception", "defoverridable", "alias", "import", "require", "use", "unquote", "unquote_splicing", "case", "cond", "for", "if", "unless", "with", "receive", "try" }; if( keywords.isFunction( target ) || elixirModuleKeyword( target ) || ( target == "quote" && !ts_node_is_null( elixirBody( role, src ) ) ) || std::find( std::begin( special ), std::end( special ), target ) != std::end( special ) ) { return false; } bool inDefault = false; for( TSNode parent = parentOf( role ); !ts_node_is_null( parent ); parent = parentOf( parent ) ) { if( elixirLiveHole( parent, role, src ) ) { break; } // outer quoted declarations do not constrain the evaluated argument if( std::strcmp( ts_node_type( parent ), "binary_operator" ) == 0 && nodeFieldText( parent, NodeField::Operator, src ) == "\\\\" ) { const TSNode value = fieldChild( parent, NodeField::Right ); inDefault = inDefault || ( !ts_node_is_null( value ) && ts_node_start_byte( role ) >= ts_node_start_byte( value ) && ts_node_end_byte( role ) <= ts_node_end_byte( value ) ); } if( !keywords.isFunction( elixirTarget( parent, src ) ) ) { continue; } TSNode head = elixirFirstArgument( parent ); if( !ts_node_is_null( head ) && std::strcmp( ts_node_type( head ), "binary_operator" ) == 0 && nodeFieldText( head, NodeField::Operator, src ) == "when" ) { head = fieldChild( head, NodeField::Left ); } if( !ts_node_is_null( head ) && ts_node_start_byte( name ) >= ts_node_start_byte( head ) && ts_node_end_byte( name ) <= ts_node_end_byte( head ) ) { return inDefault; } } return true; } // Lexical facts are collected by tags.scm, through the same cursor as every other language. // The helpers below inspect parents/fields of those captures; they never walk an independent AST. bool elixirNodeIs( TSNode node, const char* type ) noexcept { return !ts_node_is_null( node ) && std::strcmp( ts_node_type( node ), type ) == 0; } bool elixirContains( TSNode outer, TSNode inner ) noexcept { return !ts_node_is_null( outer ) && !ts_node_is_null( inner ) && ts_node_start_byte( outer ) <= ts_node_start_byte( inner ) && ts_node_end_byte( inner ) <= ts_node_end_byte( outer ); } TSNode elixirLexicalScope( TSNode node ) noexcept { for( TSNode p = parentOf( node ); !ts_node_is_null( p ); p = parentOf( p ) ) { if( elixirNodeIs( p, "source" ) || elixirNodeIs( p, "do_block" ) || elixirNodeIs( p, "stab_clause" ) || elixirNodeIs( p, "block" ) || elixirNodeIs( p, "pair" ) || elixirNodeIs( p, "else_block" ) || elixirNodeIs( p, "rescue_block" ) || elixirNodeIs( p, "catch_block" ) || elixirNodeIs( p, "after_block" ) ) { return p; } } return {}; } // The do/else/rescue/catch/after arms are siblings inside one grammar do_block, but distinct scopes. std::uint32_t elixirVisibilityEnd( TSNode scope, std::string_view src ) noexcept { if( ts_node_is_null( scope ) ) { return 0; } auto end = ts_node_end_byte( scope ); TSNode body = scope; if( elixirTarget( scope, src ) == "with" ) { body = elixirBody( scope, src ); const TSNode otherwise = elixirKeywordValue( scope, "else:", src ); if( !ts_node_is_null( otherwise ) ) { end = ts_node_start_byte( otherwise ); } } if( elixirNodeIs( body, "do_block" ) ) { for( std::uint32_t i = 0; i < ts_node_named_child_count( body ); ++i ) { const TSNode child = ts_node_named_child( body, i ); if( elixirNodeIs( child, "else_block" ) || elixirNodeIs( child, "rescue_block" ) || elixirNodeIs( child, "catch_block" ) || elixirNodeIs( child, "after_block" ) ) { return std::min( end, ts_node_start_byte( child ) ); } } } return end; } // An unquote belongs to its nearest quote body. bind_quoted disables holes by // default; an explicit literal unquote option takes precedence. Unknown options // cannot prove a live hole, so retain the inert call candidate instead. bool elixirLiveHole( TSNode hole, TSNode site, std::string_view src ) noexcept { const auto target = elixirTarget( hole, src ); if( ( target != "unquote" && target != "unquote_splicing" ) || !elixirContains( elixirFirstArgument( hole ), site ) ) { return false; } for( TSNode parent = parentOf( hole ); !ts_node_is_null( parent ); parent = parentOf( parent ) ) { if( elixirTarget( parent, src ) != "quote" || !elixirContains( elixirBody( parent, src ), hole ) ) { continue; } const TSNode option = elixirKeywordValue( parent, "unquote:", src ); if( !ts_node_is_null( option ) ) { return nodeTextOf( option, src ) == "true"; } return ts_node_is_null( elixirKeywordValue( parent, "bind_quoted:", src ) ); } return false; } bool elixirInert( TSNode node, std::string_view src ) noexcept { return inInertRegion( node, [ & ]( TSNode ancestor, TSNode site ) noexcept { const auto target = elixirTarget( ancestor, src ); if( elixirLiveHole( ancestor, site, src ) ) { return InertBoundary::Live; } if( target == "quote" && elixirContains( elixirBody( ancestor, src ), site ) ) { return InertBoundary::Inert; } return InertBoundary::None; } ); } std::string elixirFunctionName( std::string_view name, std::uint32_t arity ) { return std::string( name ) + "/" + std::to_string( arity ); } struct ElixirLexicalName { TSNode scope{}; std::uint32_t startByte = 0; std::string target; std::uint32_t endByte = 0; bool selfRelative = false; // an alias of `__MODULE__` (`alias __MODULE__, as: Current`): target names the enclosing module }; struct ElixirContext { std::string_view src; std::vector calls; std::vector identifiers; HashMap> aliases; HashMap> variables; HashMap modules; ExtractShortfall* shortfall = nullptr; // the file's sink, set by captureTagsFacts before any lookup ElixirKeywords keywords; // tags.scm's definition keywords, read by prepare (#355) std::string scopeOf( TSNode node, unsigned depth = 0 ) const { if( depth > 128 ) { ASSUME( shortfall != nullptr ); DISCLOSE( *shortfall, ExtractShortfall::DisclosureWhy::ElixirScopeTooDeep, "Elixir module nesting exceeds 128" ); return {}; } for( TSNode p = parentOf( node ); !ts_node_is_null( p ); p = parentOf( p ) ) { if( ( elixirModuleKeyword( elixirTarget( p, src ) ) || elixirTarget( p, src ) == "defimpl" ) && !elixirInert( p, src ) ) { // Quoted module declarations are AST, not the lexical module of an evaluated hole. return moduleName( p, depth + 1 ); } } return {}; } // The alias binding of the name segment `first` in force at `site` — the newest one whose visibility span and // lexical scope hold the site — or null when no alias binds it there. const ElixirLexicalName* aliasInForce( std::string_view first, TSNode site ) const { const auto found = aliases.find( std::string( first ) ); if( found == aliases.end() ) { return nullptr; } for( auto it = found->second.rbegin(); it != found->second.rend(); ++it ) { if( it->startByte <= ts_node_start_byte( site ) && ts_node_start_byte( site ) < it->endByte && elixirContains( it->scope, site ) ) { return &*it; } } return nullptr; } std::string expandAlias( std::string written, TSNode site ) const { if( written.starts_with( "Elixir." ) ) { return written.substr( 7 ); } const auto dot = written.find( '.' ); if( const ElixirLexicalName* alias = aliasInForce( std::string_view( written ).substr( 0, dot ), site ); alias != nullptr ) { return alias->target + ( dot == std::string::npos ? "" : written.substr( dot ) ); } return written; } // Whether a call's receiver names the enclosing module THROUGH an alias — `alias __MODULE__, as: Current`, then // `Current.f(x)` — so the reference is classified ElixirSelfModule and re-attributed per implementation of a // multi-target defimpl (expandElixirImplementationReferences), exactly as a literal `__MODULE__` receiver is. // Classifying from the receiver's text alone bound every implementation's `Current.f()` to the first one's f // (test/elixirnamearitycheck.sh I). A `dot` receiver is judged by its leftmost segment. bool selfRelativeAlias( TSNode receiver, TSNode site ) const { if( elixirNodeIs( receiver, "dot" ) ) { return selfRelativeAlias( fieldChild( receiver, NodeField::Left ), site ); } if( !elixirNodeIs( receiver, "alias" ) ) { return false; } const auto written = nodeTextOf( receiver, src ); if( written.starts_with( "Elixir." ) ) { return false; } const ElixirLexicalName* alias = aliasInForce( written.substr( 0, written.find( '.' ) ), site ); return alias != nullptr && alias->selfRelative; } std::string moduleOf( TSNode name, TSNode site, unsigned depth = 0 ) const { if( ts_node_is_null( name ) ) { return {}; } if( depth > 128 ) { ASSUME( shortfall != nullptr ); DISCLOSE( *shortfall, ExtractShortfall::DisclosureWhy::ElixirScopeTooDeep, "Elixir module nesting exceeds 128" ); return {}; } if( elixirNodeIs( name, "alias" ) ) { return expandAlias( std::string( nodeTextOf( name, src ) ), site ); } if( elixirNodeIs( name, "atom" ) ) { return std::string( nodeTextOf( name, src ) ); } if( nodeTextOf( name, src ) == "__MODULE__" ) { return scopeOf( site, depth + 1 ); } if( elixirNodeIs( name, "dot" ) ) { const TSNode left = fieldChild( name, NodeField::Left ); const TSNode right = fieldChild( name, NodeField::Right ); if( !elixirNodeIs( right, "alias" ) ) { return {}; } const auto prefix = moduleOf( left, site, depth + 1 ); if( !prefix.empty() ) { return prefix + "." + std::string( nodeTextOf( right, src ) ); } } return {}; } std::string moduleName( TSNode node, unsigned depth = 0 ) const { if( depth > 128 ) { ASSUME( shortfall != nullptr ); DISCLOSE( *shortfall, ExtractShortfall::DisclosureWhy::ElixirScopeTooDeep, "Elixir module nesting exceeds 128" ); return {}; } if( const auto it = modules.find( ts_node_start_byte( node ) ); it != modules.end() ) { return it->second; } const auto target = elixirTarget( node, src ); const TSNode name = elixirFirstArgument( node ); auto written = moduleOf( name, node, depth + 1 ); if( written.empty() ) { return {}; } if( target == "defimpl" ) { const TSNode forNode = elixirKeywordValue( node, "for:", src ); const auto forName = ts_node_is_null( forNode ) ? scopeOf( node, depth + 1 ) : moduleOf( forNode, node, depth + 1 ); if( elixirNodeIs( forNode, "list" ) ) { const auto names = implementationNames( node ); return names.empty() ? std::string{} : names.front(); } return forName.empty() ? std::string{} : written + "." + forName; } if( !elixirModuleKeyword( target ) ) { return {}; } const auto parent = scopeOf( node, depth + 1 ); if( elixirNodeIs( name, "alias" ) && !nodeTextOf( name, src ).starts_with( "Elixir." ) && written == nodeTextOf( name, src ) && !parent.empty() ) { written = parent + "." + written; } return written; } std::vector implementationNames( TSNode node ) const { std::vector out; if( elixirTarget( node, src ) != "defimpl" ) { return out; } const auto protocol = moduleOf( elixirFirstArgument( node ), node ); const TSNode targets = elixirKeywordValue( node, "for:", src ); if( protocol.empty() || !elixirNodeIs( targets, "list" ) ) { return out; } for( std::uint32_t i = 0; i < ts_node_named_child_count( targets ); ++i ) { if( auto target = moduleOf( ts_node_named_child( targets, i ), node ); !target.empty() ) { out.push_back( protocol + "." + target ); } } rw::sortutil::sortUnique( out ); return out; } std::vector directiveModules( TSNode node ) const { std::vector out; const TSNode first = elixirFirstArgument( node ); if( auto name = moduleOf( first, node ); !name.empty() ) { out.push_back( std::move( name ) ); return out; } if( !elixirNodeIs( first, "dot" ) ) { return out; } const auto prefix = moduleOf( fieldChild( first, NodeField::Left ), node ); const TSNode members = fieldChild( first, NodeField::Right ); if( prefix.empty() || !elixirNodeIs( members, "tuple" ) ) { return out; } for( std::uint32_t i = 0; i < ts_node_named_child_count( members ); ++i ) { const TSNode member = ts_node_named_child( members, i ); if( elixirNodeIs( member, "alias" ) ) { out.push_back( prefix + "." + std::string( nodeTextOf( member, src ) ) ); } } return out; } void bindAlias( std::string local, std::string target, TSNode node, bool selfRelative = false ) { const TSNode scope = elixirLexicalScope( node ); aliases[ std::move( local ) ].push_back( { scope, ts_node_start_byte( node ), std::move( target ), elixirVisibilityEnd( scope, src ), selfRelative } ); } // An identifier is a binding only in a pattern. The innermost matching construct defines // visibility: function parameters, clause patterns, matches and comprehension generators. TSNode bindingScope( TSNode node ) const { for( TSNode p = parentOf( node ); !ts_node_is_null( p ); p = parentOf( p ) ) { if( elixirLiveHole( p, node, src ) ) { return {}; } // patterns outside the live hole are inert syntax if( elixirNodeIs( p, "unary_operator" ) && nodeFieldText( p, NodeField::Operator, src ) == "^" ) { return {}; } if( keywords.isFunction( elixirTarget( p, src ) ) ) { TSNode head = elixirFirstArgument( p ); if( elixirNodeIs( head, "binary_operator" ) && nodeFieldText( head, NodeField::Operator, src ) == "when" ) { head = fieldChild( head, NodeField::Left ); } if( elixirNodeIs( head, "binary_operator" ) || elixirNodeIs( head, "unary_operator" ) ) { return elixirContains( head, node ) ? p : TSNode{}; } return elixirContains( elixirArguments( head ), node ) ? p : TSNode{}; } if( elixirNodeIs( p, "stab_clause" ) ) { TSNode pattern = fieldChild( p, NodeField::Left ); if( elixirNodeIs( pattern, "binary_operator" ) && nodeFieldText( pattern, NodeField::Operator, src ) == "when" ) { pattern = fieldChild( pattern, NodeField::Left ); } return elixirContains( pattern, node ) ? p : TSNode{}; } if( elixirNodeIs( p, "binary_operator" ) ) { const auto op = nodeFieldText( p, NodeField::Operator, src ); if( op == "=" || op == "<-" || op == "\\\\" ) { if( !elixirContains( fieldChild( p, NodeField::Left ), node ) ) { // The right of `<-` is the generator's expression and the right of `\\` a default // value: neither binds. The right of `=` binds exactly when the whole match sits in a // PATTERN — `def join(%Socket{} = socket, _)`, `%S{} = s -> …`, `{:ok, %{} = m} <- x` — // and only the construct above can say so, so the walk carries on upward instead of // deciding here. A body-level `x = y` reaches the def head, is outside it, and stays a // call. Returning empty from here turned every such `socket` into a zero-arity call // (PR #81 review item 3: 12 false callers of a socket/0; test/elixirnamearitycheck.sh B). if( op != "=" ) { return {}; } continue; } if( op == "<-" ) { for( TSNode c = parentOf( p ); !ts_node_is_null( c ); c = parentOf( c ) ) { if( elixirTarget( c, src ) == "for" || elixirTarget( c, src ) == "with" ) { return c; } } } if( op != "\\\\" ) { return elixirLexicalScope( p ); } } } } return {}; } // `&name/N`: name is the operand of a named capture — a function, whatever variable shares its spelling bool namedCapture( TSNode name ) const { const TSNode slash = parentOf( name ); return elixirNodeIs( slash, "binary_operator" ) && nodeFieldText( slash, NodeField::Operator, src ) == "/" && elixirNodeIs( parentOf( slash ), "unary_operator" ) && nodeFieldText( parentOf( slash ), NodeField::Operator, src ) == "&"; } bool bareCall( TSNode name ) const { const auto text = nodeTextOf( name, src ); if( text.empty() || text == "true" || text == "false" || text == "nil" ) { return false; } const TSNode parent = parentOf( name ); if( elixirNodeIs( parent, "dot" ) || ( elixirNodeIs( parent, "call" ) && ts_node_eq( fieldChild( parent, NodeField::Target ), name ) ) ) { return false; } // A capture names a function even if a variable with the same spelling is bound — and even when the spelling // starts with `_`: the underscore marks an UNUSED VARIABLE, and a named capture is never one, so `&_seed/0` // names _seed/0. This ran after the underscore rule below and the capture minted nothing, uncounted // (test/elixirnamearitycheck.sh J). A bare `_seed` read still falls to that rule. if( namedCapture( name ) ) { return true; } if( text.front() == '_' || !ts_node_is_null( bindingScope( name ) ) ) { return false; } if( const auto it = variables.find( std::string( text ) ); it != variables.end() ) { for( const auto& variable : it->second ) { if( variable.startByte <= ts_node_start_byte( name ) && ts_node_start_byte( name ) < variable.endByte && elixirContains( variable.scope, name ) ) { return false; } } } return true; } void prepare( TSQueryCursor* cursor, TSQuery* query, TSNode root, std::string_view bytes, std::uint32_t fileId, std::vector& binds, std::vector& includes, std::vector& refs ) { src = bytes; keywords.function = predicateStrings( query, "any-of?", "_keyword" ); keywords.metadata = predicateStrings( query, "not-any-of?", "_attribute" ); ts_query_cursor_exec( cursor, query, root ); TSQueryMatch match; while( ts_query_cursor_next_match( cursor, &match ) ) { if( !passesStringPredicates( query, match, src ) ) { continue; // the same query, the same predicates: one meaning for a pattern in both passes (#355) } for( std::uint16_t i = 0; i < match.capture_count; ++i ) { const auto& capture = match.captures[ i ]; std::uint32_t size = 0; const char* name = ts_query_capture_name_for_id( query, capture.index, &size ); const std::string_view label( name, size ); if( label == "elixir.context" && !elixirInert( capture.node, src ) ) { calls.push_back( capture.node ); } if( label == "reference.bare" && !elixirInert( capture.node, src ) ) { identifiers.push_back( capture.node ); } } } std::sort( calls.begin(), calls.end(), []( TSNode a, TSNode b ) { return ts_node_start_byte( a ) < ts_node_start_byte( b ); } ); aliases.reserve( calls.size() / 8 + 1 ); variables.reserve( identifiers.size() / 2 + 1 ); modules.reserve( calls.size() / 8 + 1 ); for( TSNode call : calls ) { const auto keyword = elixirTarget( call, src ); if( elixirModuleKeyword( keyword ) || keyword == "defimpl" ) { auto full = moduleName( call ); if( full.empty() ) { continue; } modules.emplace( ts_node_start_byte( call ), full ); if( keyword == "defimpl" ) { RawRef ref; ref.fileId = fileId; ref.startByte = ts_node_start_byte( call ); ref.line = ts_node_start_point( call ).row + 1; ref.lang = Lang::Elixir; ref.role = RefRole::Extends; ref.isInherit = true; ref.name = moduleOf( elixirFirstArgument( call ), call ); refs.push_back( std::move( ref ) ); } if( keyword != "defimpl" ) { const TSNode name = elixirFirstArgument( call ); const auto written = nodeTextOf( name, src ); if( elixirNodeIs( name, "alias" ) && !written.starts_with( "Elixir." ) ) { // Kernel.defmodule/2, nesting: a declaration inside Outer binds the FIRST segment of its written // name in the enclosing scope from here on — `defmodule Inner` binds Inner -> Outer.Inner (the // module itself); `defmodule Inner.Deep` defines Outer.Inner.Deep and binds Inner -> Outer.Inner, // so a later `Inner.Deep.f()` names the nested module even when a top-level Inner.Deep exists. // Exactly when moduleName nested it (`full` is the parent's name plus the written one) — a // top-level or alias-expanded name binds nothing new. The dotted shape bound nothing at all, // and the later call reached the top-level module, or nothing (test/elixirnamearitycheck.sh H). const auto dot = written.find( '.' ); if( dot == std::string_view::npos ) { bindAlias( std::string( written ), full, call ); } else if( const auto parent = scopeOf( call ); !parent.empty() && full == parent + "." + std::string( written ) ) { const std::string first( written.substr( 0, dot ) ); bindAlias( first, parent + "." + first, call ); } } } continue; } const bool behaviour = ( keyword == "behaviour" || keyword == "behavior" ) && elixirAttribute( parentOf( call ), src ) == keyword; if( keyword != "alias" && keyword != "import" && keyword != "require" && keyword != "use" && !behaviour ) { continue; } const auto targets = directiveModules( call ); for( const std::string& target : targets ) { Include include; include.fileId = fileId; include.target = target; include.byte = ts_node_start_byte( call ); includes.push_back( std::move( include ) ); RawRef ref; ref.fileId = fileId; ref.startByte = ts_node_start_byte( call ); ref.line = ts_node_start_point( call ).row + 1; ref.lang = Lang::Elixir; ref.role = behaviour ? RefRole::Extends : RefRole::Import; ref.isInherit = behaviour; ref.name = target; refs.push_back( std::move( ref ) ); const TSNode as = elixirKeywordValue( call, "as:", src ); if( keyword == "alias" || ( keyword == "require" && !ts_node_is_null( as ) ) ) { const auto dot = target.rfind( '.' ); const auto local = ts_node_is_null( as ) ? target.substr( dot == std::string::npos ? 0 : dot + 1 ) : std::string( nodeTextOf( as, src ) ); // `alias __MODULE__[.Sub], as: X` names the enclosing module: the binding carries that, so a call // through X is classified like a literal `__MODULE__` receiver (selfRelativeAlias) const bool selfRelative = nodeTextOf( elixirFirstArgument( call ), src ).starts_with( "__MODULE__" ); if( local != "false" ) { bindAlias( local, target, call, selfRelative ); } } if( keyword != "import" ) { continue; } RawBind bind; bind.fileId = fileId; bind.lang = Lang::Elixir; bind.kind = LocalBindKind::ElixirImport; bind.startByte = ts_node_start_byte( call ); bind.spanStart = ts_node_end_byte( call ); const TSNode lexical = elixirLexicalScope( call ); bind.spanEnd = ts_node_is_null( lexical ) ? bind.spanStart : elixirVisibilityEnd( lexical, src ); bind.typeName = target; TSNode filter = elixirKeywordValue( call, "only:", src ); bind.var = "only"; if( ts_node_is_null( filter ) ) { filter = elixirKeywordValue( call, "except:", src ); bind.var = ts_node_is_null( filter ) ? "all" : "except"; } if( elixirNodeIs( filter, "atom" ) ) { bind.var = std::string( nodeTextOf( filter, src ).substr( 1 ) ); } else if( elixirNodeIs( filter, "list" ) ) { std::string pairs; // built here, interned once (MEM-B1: a pool name's += would intern every prefix) for( std::uint32_t i = 0; i < ts_node_named_child_count( filter ); ++i ) { const TSNode entries = ts_node_named_child( filter, i ); if( !elixirNodeIs( entries, "keywords" ) ) { continue; } for( std::uint32_t j = 0; j < ts_node_named_child_count( entries ); ++j ) { const TSNode pair = ts_node_named_child( entries, j ); auto key = nodeTextOf( fieldChild( pair, NodeField::Key ), src ); while( !key.empty() && ( key.back() == ':' || std::isspace( static_cast( key.back() ) ) ) ) { key.remove_suffix( 1 ); } const auto count = nodeTextOf( fieldChild( pair, NodeField::Value ), src ); pairs += "\n" + std::string( key ) + "/" + std::string( count ) + "\n"; } } bind.importedName = pairs; } binds.push_back( bind ); } } for( TSNode identifier : identifiers ) { const TSNode scope = bindingScope( identifier ); if( ts_node_is_null( scope ) ) { continue; } auto visibleFrom = ts_node_start_byte( identifier ); for( TSNode p = parentOf( identifier ); !ts_node_is_null( p ) && !ts_node_eq( p, scope ); p = parentOf( p ) ) { const auto op = nodeFieldText( p, NodeField::Operator, src ); if( ( op == "=" || op == "<-" ) && elixirContains( fieldChild( p, NodeField::Left ), identifier ) ) { visibleFrom = ts_node_end_byte( p ); // the RHS runs before the new binding exists break; } } variables[ std::string( nodeTextOf( identifier, src ) ) ].push_back( { scope, visibleFrom, {}, elixirVisibilityEnd( scope, src ) } ); } } }; // `defimpl P, for: [A, B]` has one written body and two real modules. Duplicate its source // facts before IDs are assigned; reference attribution below fans its shared spans out likewise. void elixirExpandImplementations( const ElixirContext& context, std::vector& defs, std::size_t firstDef, std::vector& binds, std::size_t firstBind ) { // `text` is a std::string (a def's scope / name) or a pool name (a bind's typeName, MEM-B1): build the new text, assign once const auto replacePrefix = []( auto& text, const std::string& from, const std::string& to ) { const std::string_view was = text; if( was == from || ( was.starts_with( from ) && was.size() > from.size() && was[ from.size() ] == '.' ) ) { text = to + std::string( was.substr( from.size() ) ); } }; for( TSNode call : context.calls ) { const auto names = context.implementationNames( call ); if( names.size() < 2 ) { continue; } const auto begin = ts_node_start_byte( call ), end = ts_node_end_byte( call ); const auto defEnd = defs.size(), bindEnd = binds.size(); for( std::size_t i = 1; i < names.size(); ++i ) { for( std::size_t j = firstDef; j < defEnd; ++j ) { if( defs[j].startByte < begin || defs[j].endByte > end ) { continue; } RawDef clone = defs[j]; replacePrefix( clone.scope, names.front(), names[i] ); replacePrefix( clone.name, names.front(), names[i] ); defs.push_back( std::move( clone ) ); } for( std::size_t j = firstBind; j < bindEnd; ++j ) { if( binds[j].startByte < begin || binds[j].startByte >= end || binds[j].kind == LocalBindKind::ElixirImport ) { continue; } RawBind clone = binds[j]; replacePrefix( clone.typeName, names.front(), names[i] ); binds.push_back( clone ); } } } } // Explicit call, bare zero-arity call, pipe and &name/arity all use one counting rule. std::pair elixirCallArity( TSNode role, TSNode name, std::string_view src ) noexcept { TSNode expression = role; if( elixirNodeIs( role, "binary_operator" ) && nodeFieldText( role, NodeField::Operator, src ) == "|>" ) { expression = name; } const TSNode parent = parentOf( expression ); if( elixirNodeIs( parent, "binary_operator" ) && nodeFieldText( parent, NodeField::Operator, src ) == "/" && nodeFieldText( parentOf( parent ), NodeField::Operator, src ) == "&" ) { const auto arity = nodeFieldText( parent, NodeField::Right, src ); std::uint16_t count = 0; const auto parsed = std::from_chars( arity.data(), arity.data() + arity.size(), count ); return { count, parsed.ec == std::errc{} && parsed.ptr == arity.data() + arity.size() }; } const TSNode args = elixirArguments( expression ); std::uint32_t count = 0; if( !ts_node_is_null( args ) ) { for( std::uint32_t i = 0; !ts_node_is_null( args ) && i < ts_node_named_child_count( args ); ++i ) { if( !elixirNodeIs( ts_node_named_child( args, i ), "comment" ) ) { ++count; } } } for( std::uint32_t i = 0; i < ts_node_named_child_count( expression ); ++i ) { if( elixirNodeIs( ts_node_named_child( expression, i ), "do_block" ) ) { ++count; } } if( elixirNodeIs( parent, "binary_operator" ) && nodeFieldText( parent, NodeField::Operator, src ) == "|>" && ts_node_eq( fieldChild( parent, NodeField::Right ), expression ) ) { ++count; } return { std::uint16_t( std::min( count, 65535u ) ), count <= 65535u }; } /// Count the `\\` default parameters in a definition head; 0 for a parameterless or bodyless head. A clause's /// arity is otherwise EXACT — Elixir has no variadic form — which is what makes it call-comparable (arityExact). std::uint32_t elixirHeadDefaultCount( TSNode node, std::string_view src ) noexcept { TSNode head = elixirFirstArgument( node ); if( elixirNodeIs( head, "binary_operator" ) && nodeFieldText( head, NodeField::Operator, src ) == "when" ) { head = fieldChild( head, NodeField::Left ); } const TSNode args = elixirArguments( head ); std::uint32_t defaults = 0; for( std::uint32_t i = 0; !ts_node_is_null( args ) && i < ts_node_named_child_count( args ); ++i ) { if( nodeFieldText( ts_node_named_child( args, i ), NodeField::Operator, src ) == "\\\\" ) { ++defaults; } } return defaults; } void elixirDefinitionFacts( const RawDef& def, TSNode node, std::string_view src, std::vector& binds, const ElixirKeywords& keywords ) { const auto keyword = elixirTarget( node, src ); if( !keywords.isFunction( keyword ) ) { return; } RawBind bind; bind.fileId = def.fileId; bind.startByte = def.startByte; bind.lang = Lang::Elixir; bind.kind = LocalBindKind::ElixirCallable; bind.var = def.name; bind.typeName = def.scope; bind.importedName = keyword; bind.spanStart = def.startByte; bind.spanEnd = def.endByte; binds.push_back( bind ); const std::uint32_t defaults = elixirHeadDefaultCount( node, src ); const auto name = std::string_view( def.name ).substr( 0, def.name.rfind( '/' ) ); for( std::uint32_t count = 1; count <= defaults && count <= def.params; ++count ) { bind.kind = LocalBindKind::ElixirDefault; bind.var = elixirFunctionName( name, def.params - count ); bind.importedName = def.name; binds.push_back( bind ); } } } // namespace } // namespace rw