Skip to content

class Crystal::Program
inherits Crystal::NonGenericModuleType

A program contains all types and top-level methods related to one compilation of a program.

It also carries around all information needed to compile a bunch of files: the unions, the symbols used, all global variables, all required files, etc. Because of this, a Program is usually passed around in every step of a compilation to record and query this information.

In a way, a Program is an alternative implementation to having global variables for all of this data, but modeled this way one can easily test and exercise programs because each one has its own definition of the types created, methods instantiated, etc.

Additionally, a Program acts as a regular type (a module) that can have types (the top-level types) and methods (the top-level methods), and which can also include other modules (this happens when you do include Module at the top-level).

Included modules

Crystal::DefInstanceContainer

Constants

PKG_CONFIG_PATH = Process.find_executable("pkg-config")

Class methods

Methods

#add_def(node : Def)

View source

#add_finished_hook(scope, a_macro, node)

View source

#add_macro(node : Macro)

View source

#add_type(set, type : Nil)

View source

#add_type(types, type : Type)

View source

#add_type(types, type : VoidType)

When Void participates in a union, it becomes Nil (users shouldn't deal with real Void values)

View source

#add_type(types, type : AliasType)

View source

#add_type(types, type : UnionType)

View source

#after_inference_types : Set(Crystal::Type)

Types that have instance vars initializers which need to be visited (transformed) by CleanupTransformer once the semantic analysis finishes.

Todo

this probably isn't needed and we can just traverse all types at the end, and analyze all instance variables initializers that we found. This should simplify a bit of code.

View source

#always_inline_annotation

View source

#argc : Const

The constant for ARGC_UNSAFE

View source

#argc? : Const?

The constant for ARGC_UNSAFE

View source

#argv : Const

The constant for ARGV_UNSAFE

View source

#argv? : Const?

The constant for ARGV_UNSAFE

View source

#array

View source

#array_of(type)

Returns the Type for Array(type)

View source

#bits64?

View source

#bool

View source

#cache_dir=(cache_dir : String?)

The cache directory where temporary files are placed.

View source

#call_convention_annotation

View source

#char

View source

#check_call_to_deprecated_macro(a_macro : Macro, call : Call)

View source

#check_call_to_deprecated_method(node : Call)

View source

#check_private(node)

View source

#class_type

Returns the Class type

View source

#class_var_initializers : Array(Crystal::ClassVarInitializer)

The class var initializers stored to be used by the cleanup transformer

View source

#cleanup(node)

View source

#cleanup_files

View source

#cleanup_single_type(type, transformer)

View source

#cleanup_type(type, transformer)

View source

#cleanup_types

View source

#codegen(node, single_module = false, debug = Debug::Default)

View source

#codegen_target=(codegen_target)

View source

#color=(color)

If true, error messages can be colorized

View source

#color? : Bool

If true, error messages can be colorized

View source

#colorize(obj)

Colorizes the given object, depending on whether this program is configured to use colors.

View source

#combined_union_of(types : Array)

View source

#compact_types

View source

#compact_types(types)

View source

#compiled_macros_cache=(compiled_macros_cache)

View source

#compiler : Compiler?

View source

#compiler=(compiler : Compiler?)

View source

#const_initializers : Array(Crystal::Const)

Here we store constants, in the order that they are used. They will be initialized as soon as the program starts, before the main code.

View source

#crystal

View source

#crystal_path

Returns a CrystalPath for this program.

View source

#define_default_new(type)

View source

#define_default_new_single(type)

View source

#define_new_methods(new_expansions)

View source

#deprecated_annotation

View source

#enum

View source

#error_on_warnings : Bool

If true compiler will error if warnings are found.

View source

#error_on_warnings=(error_on_warnings : Bool)

If true compiler will error if warnings are found.

View source

#evaluate(node, debug = Debug::Default)

View source

#exception

View source

#expand_macro(a_macro : Macro, call : Call, scope : Type, path_lookup : Type? = nil, a_def : Def? = nil)

View source

#expand_macro(node : ASTNode, scope : Type, path_lookup : Type? = nil, free_vars = nil, a_def : Def? = nil)

View source

#experimental_annotation

View source

#extern_annotation

View source

#file_module(filename)

View source

#file_module?(filename)

View source

#file_modules : Hash(String, Crystal::FileModule)

All FileModules indexed by their filename. These store file-private defs, and top-level variables in files other than the main file.

View source

#filename : String?

The main filename of this program

View source

#filename=(filename : String?)

The main filename of this program

View source

#find_in_path(filename, relative_to = nil) : Array(String)?

Finds filename in the configured CRYSTAL_PATH for this program, relative to relative_to.

View source

#flags

Returns the flags for this program. By default these are computed from the target triple (for example x86_64, darwin, linux, etc.), but can be overwritten with flags= and also added with the -D command line argument.

See Compiler#flags.

View source

#flags_annotation

View source

#float

View source

#float32

View source

#float64

View source

#global_vars : Hash(String, Crystal::MetaTypeVar)

All global variables in the program ($foo, $bar), indexed by their name. The names includes the $ sign.

View source

#has_flag?(name : String)

Returns true if name is in the program's flags.

View source

#hash_of(key_type, value_type)

View source

#hash_type

Returns the Hash type

View source

#host_compiler

Creates a compiler instance with the host as target used for macro_run compilation.

View source

#host_flags

View source

#ignore_warning_due_to_location?(location : Location?)

View source

#instance_offset_of(type, element_index)

View source

#instance_size_of(type)

View source

#int128

View source

#int16

View source

#int32

View source

#int64

View source

#int8

View source

#int?(int)

Returns the IntegerType that matches the given Int value

View source

#int_type(signed, size)

View source

#lib_flags

View source
View source

Returns every @[Link] annotation in the program parsed as LinkAnnotation

View source

#literal_expander

Returns a LiteralExpander useful to expand literal like arrays and hashes into simpler forms.

View source

#llvm_id

View source

#llvm_typer

View source

#lookup_path_item(name : String, lookup_in_namespace, include_private, location)

Looks up a single path item relative to *self`.

If lookup_in_namespace is true, if the type is not found in self or self's parents, the path item is searched in this type's namespace. This parameter is useful because when writing Foo::Bar::Baz, Foo should be searched in enclosing namespaces, but Bar and Baz not.

View source

#lookup_private_matches(filename, signature, analyze_all = false)

View source

#macro_compile(filename)

View source

#macro_expander

Returns a MacroExpander to expand macro code into crystal code.

View source

#macro_run(filename, args)

View source

#memcpy(llvm_mod, llvm_context)

View source

#memset(llvm_mod, llvm_context)

View source

#metaclass

Returns this type's metaclass, which holds class methods for this type.

View source

#naked_annotation

View source

#named_tuple

View source

#named_tuple_of(entries : Hash(String, Type) | NamedTuple)

Returns the Type for NamedTuple(**entries)

View source

#named_tuple_of(entries : Array(NamedArgumentType))

Returns the Type for NamedTuple(**entries)

View source

#new_temp_var : Var

View source

#new_temp_var_name

View source

#nil_type

Returns the Nil type

View source

#nil_var

Returns a Var that has Nil as a type. This variable is bound to other nodes in the semantic phase for things that need to be nilable, for example to a variable that's only declared in one branch of an if expression.

View source

#nilable(type)

Returns the Type for type | Nil

View source

#no_inline_annotation

View source

#no_return

View source

#normalize(node, inside_exp = false, current_def = nil)

View source

#number

View source

#object

View source

#object_extension

View source

#offset_of(type, element_index)

View source

#packed_annotation

View source

#parse_macro_source(generated_source, macro_expansion_pragmas, the_macro, node, vars, current_def = nil, inside_type = false, inside_exp = false, visibility : Visibility = :public

View source

#parse_macro_source(generated_source, macro_expansion_pragmas, the_macro, node, vars, current_def = nil, inside_type = false, inside_exp = false, mode : Parser::ParseMode = :normal, visibility : Visibility = :public)

View source

#pointer

View source

#pointer_of(type)

Returns the Type for Pointer(type)

View source

#predefined_constants : Array(Crystal::Const)

View source

#primitive_annotation

View source

#printf(llvm_mod, llvm_context)

View source

#proc

View source

#proc_of(nodes : Array(ASTNode), return_type : Type)

Returns the Type for Proc(*nodes.map(&.type), return_type)

View source

#proc_of(types : Array)

Returns the Type for Proc(*types)

View source

#process_finished_hooks(visitor)

Visits all finished hooks with the given visitor

View source

#progress_tracker : Crystal::ProgressTracker

A ProgressTracker object which tracks compilation progress.

View source

#progress_tracker=(progress_tracker)

A ProgressTracker object which tracks compilation progress.

View source

#raises_annotation

View source

#range

View source

#range_of(begin_type, end_type)

View source

#realloc(llvm_mod, llvm_context)

View source

#record_require(filename, relative_to) : Nil

Remembers that the program depends on this require.

View source

#recorded_requires=(recorded_requires)

View source

#reference

View source

#regex

View source

#report_warning(node : ASTNode, message : String)

View source

#report_warning_at(location : Location?, message : String)

View source

#requires : Set(String)

All required files. The set stores absolute files. This way files loaded by require nodes are only processed once.

View source

#returns_twice_annotation

View source

#run(code, filename = nil, debug = Debug::Default)

View source

#semantic(node : ASTNode, cleanup = true) : ASTNode

Runs semantic analysis on the given node, returning a node that's typed. In the process types and methods are defined in this program.

View source

#show_error_trace=(show_error_trace)

Whether to show error trace

View source

#show_error_trace? : Bool

Whether to show error trace

View source

#size_of(type)

View source

#splat_expansions : Hash(Def, Type)

Hash that prevents recursive splat expansions. For example:

def foo(*x)
  foo(x)
end

foo(1)

Here x will be {Int32}, then {{Int32}}, etc.

The way we detect this is by remembering the type of the splat, associated to a def's object id (the UInt64), and on an instantiation we compare the new type with the previous one and check if it contains the previous type.

View source

#static_array

View source

#static_array_of(type, size)

Returns the Type for StaticArray(type, size)

View source

#stdout : IO

Default standard output to use in a program, while compiling.

View source

#stdout=(stdout : IO)

Default standard output to use in a program, while compiling.

View source

#string

View source

#string_pool : StringPool

A String pool to avoid creating the same strings over and over. This pool is passed to the parser, macro expander, etc.

View source

#struct

Returns the Struct type

View source

#symbol

View source

#symbols : Set(String)

All symbols (:foo, :bar) found in the program

View source

#target_machine : LLVM::TargetMachine

View source

#target_machine=(target_machine : LLVM::TargetMachine)

View source

#tempfiles : Array(String)

Temporary files which are generated by macro runs that need to be deleted after the compilation is finished.

View source

#thread_local_annotation

View source

#to_s(io : IO) : Nil

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>
View source

#top_level_semantic(node)

Processes type declarations and instance/class/global vars types are guessed or followed according to type annotations.

This alone is useful for some tools like doc or hierarchy where a full semantic of the program is not needed.

View source

#tuple

View source

#tuple_of(types)

Returns the Type for Tuple(*types)

View source

#type_combine(types)

View source

#type_desc

View source

#type_from_literal_kind(kind)

View source

#type_merge(types : Array(Type?))

View source

#type_merge(nodes : Array(ASTNode))

View source

#type_merge_two(first, second)

View source

#type_merge_union_of(types : Array(Type))

View source

#uint128

View source

#uint16

View source

#uint32

View source

#uint64

View source

#uint8

View source

#undefined_class_variable(node, owner, similar_name)

View source

#undefined_global_variable(node, similar_name)

View source

#undefined_instance_variable(node, owner, similar_name)

View source

#undefined_variable_message(kind, example_name)

View source

#union

View source

#union_of(type1, type2)

Returns the Type for type1 | type2

View source

#union_of(types : Array)

Returns the Type for Union(*types)

View source

#unions : Hash(Array(UInt64), Crystal::UnionType)

All created unions in a program, indexed by an array of opaque ids of each type in the union. The array (the key) is sorted by this opaque id.

A program caches them this way so a union of String | Int32 or Int32 | String is represented by a single, unique type in the program.

View source

#value

View source

#vars : Hash(String, Crystal::MetaVar)

Top-level variables found in a program (only in the main file).

View source

#visit_class_vars_initializers(node)

View source

#visit_main(node, visitor = MainVisitor.new(self), process_finished_hooks = false, cleanup = true)

View source

#visit_with_finished_hooks(node, visitor)

View source

#void

View source

#wants_doc=(wants_doc)

If true, doc comments are attached to types and methods.

View source

#wants_doc? : Bool

If true, doc comments are attached to types and methods.

View source

#warning_failures : Array(String)

Detected warning failures.

View source

#warning_failures=(warning_failures)

Detected warning failures.

View source

#warnings : Warnings

Which kind of warnings wants to be detected.

View source

#warnings=(warnings : Warnings)

Which kind of warnings wants to be detected.

View source

#warnings_exclude : Array(String)

Paths to ignore for warnings detection.

View source

#warnings_exclude=(warnings_exclude : Array(String))

Paths to ignore for warnings detection.

View source