Ameba::AST::Scope - ameba 1.7.0

# class Ameba::AST::Scope

- Ameba::AST::Scope
- Reference
- Object

##
Overview

Represents a context of the local variable visibility. This is where the local variables belong to.

##
Defined in:

ameba/ast/scope.cr

##
Constructors

- .new(node : Crystal::ASTNode, outer_scope : Nil | Ameba::AST::Scope = nil)

  Creates a new scope.

##
Instance Method Summary

- #==(other : self)

  Returns `true` if this reference is the same as other.

- #add_argument(node)

  Creates a new argument in the current scope.

- #add_ivariable(node)

  Adds a new instance variable to the current scope.

- #add_type_dec_variable(node)

  Adds a new type declaration variable to the current scope.

- #add_variable(node)

  Creates a new variable in the current scope.

- #arg?(var)

  Returns `true` if var is an argument in current scope, `false` otherwise.

- #arguments : Array(Ameba::AST::Argument)

  Link to the arguments in current scope

- #assign_variable(name, node)

  Creates a new assignment for the variable.

- #assigns_ivar?(name)

  Returns `true` if instance variable is assigned in this scope.

- #assigns_type_dec_variable?(name)

  Returns `true` if type declaration variable is assigned in this scope.

- #block?

  Returns `true` if current scope represents a block (or proc), `false` otherwise.

- #class_def?(*, check_outer_scopes = false)

  Returns `true` if current scope is a class def, `false` otherwise.

- #declared_at?(name : String, node) : Bool

  Returns `true` if a local variable named name has a definition that reaches the program point where node (an inner scope's node) is introduced.

- #def?(*, check_outer_scopes = false)

  Returns `true` if current scope is a def, `false` otherwise.

- #end_location(*args, **options)
- #end_location(*args, **options, &)
- #enum_def?(*, check_outer_scopes = false)

  Returns `true` if current scope is a enum def, `false` otherwise.

- #eql?(node)

  Returns `true` if the node represents exactly the same Crystal node as `@node`.

- #find_variable(name : String)

  Returns variable by its name or `nil` if it does not exist.

- #fun_def?(*, check_outer_scopes = false)

  Returns `true` if current scope is a fun def, `false` otherwise.

- #hash(hasher)

  See `Object#hash(hasher)`

- #in_macro?

  Returns `true` if current scope sits inside a macro.

- #inherited?
- #inherited?(&)
- #inner_scopes : Array(Ameba::AST::Scope)

  List of inner scopes

- #ivariables : Array(Ameba::AST::InstanceVariable)

  Link to the instance variables used in current scope

- #lib_def?(*, check_outer_scopes = false)

  Returns `true` if current scope is a lib def, `false` otherwise.

- #location(*args, **options)
- #location(*args, **options, &)
- #module_def?(*, check_outer_scopes = false)

  Returns `true` if current scope is a module def, `false` otherwise.

- #node : Crystal::ASTNode

  The actual AST node that represents a current scope.

- #outer_scope : Scope | Nil

  Link to the outer scope

- #references : Array(Ameba::AST::Reference)

  Link to all variable references in current scope

- #references?(variable : Variable, check_inner_scopes = true)

  Returns `true` if current scope (or any of inner scopes) references variable, `false` otherwise.

- #spawn_block?

  Returns `true` if current scope represents a spawn block, e.

- #to_s(*args, **options)
- #to_s(*args, **options, &)
- #top_level?

  Returns `true` if this scope is a top level scope, `false` otherwise.

- #type_dec_variables : Array(Ameba::AST::TypeDecVariable)

  Link to the type declaration variables used in current scope

- #type_definition?

  Returns `true` if and only if current scope represents some type definition, for example a class.

- #variables : Array(Ameba::AST::Variable)

  Link to local variables

- #visibility

  Returns visibility of the current scope (could be inherited from the outer scope).

- #visibility=(visibility : Crystal::Visibility | Nil)

  Scope visibility level

- #yields=(yields : Bool)

  Whether the scope yields.

- #yields?(check_inner_scopes = true)

  Returns `true` if current scope (or any of inner scopes) yields, `false` otherwise.

##
Constructor Detail

def self.new(node : Crystal::ASTNode, outer_scope : Nil | Ameba::AST::Scope = nil) #

Creates a new scope. Accepts the AST node and the outer scope.

    scope = Scope.new(class_node, nil)

##
Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns `true` if this reference is the same as other. Invokes `same?`.

def add_argument(node) #

Creates a new argument in the current scope.

    scope = Scope.new(class_node, nil)
    scope.add_argument(arg_node)

def add_ivariable(node) #

Adds a new instance variable to the current scope.

    scope = Scope.new(class_node, nil)
    scope.add_ivariable(ivar_node)

def add_type_dec_variable(node) #

Adds a new type declaration variable to the current scope.

    scope = Scope.new(class_node, nil)
    scope.add_type_dec_variable(node)

def add_variable(node) #

Creates a new variable in the current scope.

    scope = Scope.new(class_node, nil)
    scope.add_variable(var_node)

def arg?(var) #

Returns `true` if var is an argument in current scope, `false` otherwise.

def arguments : Array(Ameba::AST::Argument) #

Link to the arguments in current scope

def assign_variable(name, node) #

Creates a new assignment for the variable.

    scope = Scope.new(class_node, nil)
    scope.assign_variable(var_name, assign_node)

def assigns_ivar?(name) #

Returns `true` if instance variable is assigned in this scope.

def assigns_type_dec_variable?(name) #

Returns `true` if type declaration variable is assigned in this scope.

def block? #

Returns `true` if current scope represents a block (or proc), `false` otherwise.

def class_def?(*, check_outer_scopes = false) #

Returns `true` if current scope is a class def, `false` otherwise.

def declared_at?(name : String, node) : Bool #

Returns `true` if a local variable named name has a definition that reaches the program point where node (an inner scope's node) is introduced.

    scope.declared_at?("foo", block_node)

def def?(*, check_outer_scopes = false) #

Returns `true` if current scope is a def, `false` otherwise.

def end_location(*args, **options) #
def end_location(*args, **options, &) #
def enum_def?(*, check_outer_scopes = false) #

Returns `true` if current scope is a enum def, `false` otherwise.

def eql?(node) #

Returns `true` if the node represents exactly the same Crystal node as `@node`.

def find_variable(name : String) #

Returns variable by its name or `nil` if it does not exist.

    scope = Scope.new(class_node, nil)
    scope.find_variable("foo")

def fun_def?(*, check_outer_scopes = false) #

Returns `true` if current scope is a fun def, `false` otherwise.

def hash(hasher) #
Description copied from class Reference

See `Object#hash(hasher)`

def in_macro? #

Returns `true` if current scope sits inside a macro.

def inherited? #
def inherited?(&) #
def inner_scopes : Array(Ameba::AST::Scope) #

List of inner scopes

def ivariables : Array(Ameba::AST::InstanceVariable) #

Link to the instance variables used in current scope

def lib_def?(*, check_outer_scopes = false) #

Returns `true` if current scope is a lib def, `false` otherwise.

def location(*args, **options) #
def location(*args, **options, &) #
def module_def?(*, check_outer_scopes = false) #

Returns `true` if current scope is a module def, `false` otherwise.

def node : Crystal::ASTNode #

The actual AST node that represents a current scope.

def outer_scope : Scope | Nil #

Link to the outer scope

def references : Array(Ameba::AST::Reference) #

Link to all variable references in current scope

def references?(variable : Variable, check_inner_scopes = true) #

Returns `true` if current scope (or any of inner scopes) references variable, `false` otherwise.

def spawn_block? #

Returns `true` if current scope represents a spawn block, e. g.

    spawn do
      # ...
    end

def to_s(*args, **options) #
def to_s(*args, **options, &) #
def top_level? #

Returns `true` if this scope is a top level scope, `false` otherwise.

def type_dec_variables : Array(Ameba::AST::TypeDecVariable) #

Link to the type declaration variables used in current scope

def type_definition? #

Returns `true` if and only if current scope represents some type definition, for example a class.

def variables : Array(Ameba::AST::Variable) #

Link to local variables

def visibility #

Returns visibility of the current scope (could be inherited from the outer scope).

def visibility=(visibility : Crystal::Visibility | Nil) #

Scope visibility level

def yields=(yields : Bool) #

Whether the scope yields.

def yields?(check_inner_scopes = true) #

Returns `true` if current scope (or any of inner scopes) yields, `false` otherwise.