class Crystal::Compiler

Overview

Main interface to the compiler.

A Compiler parses source code, type checks it and optionally generates an executable.

Defined in:

compiler/crystal/compiler.cr

Constant Summary

DEFAULT_LINKER = ENV["CC"]? || ({{ (env("CRYSTAL_CONFIG_CC")) || "cc" }})
MSVC_LINKER = ENV["CC"]? || ({{ (env("CRYSTAL_CONFIG_CC")) || "cl.exe" }})

Instance Method Summary

Instance methods inherited from class Reference

==(other : self)
==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup, hash(hasher) hash, initialize initialize, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference) : Bool
same?(other : Nil)
same?
, to_s(io : IO) : Nil to_s

Constructor methods inherited from class Reference

new new, unsafe_construct(address : Pointer, *args, **opts) : self unsafe_construct

Class methods inherited from class Reference

pre_initialize(address : Pointer) pre_initialize

Instance methods inherited from class Object

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash
hash
, in?(collection : Object) : Bool
in?(*values : Object) : Bool
in?
, inspect(io : IO) : Nil
inspect : String
inspect
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil!
not_nil!
, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String
to_json
, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil
to_pretty_json
, to_s(io : IO) : Nil
to_s : String
to_s
, to_yaml(io : IO) : Nil
to_yaml : String
to_yaml
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

from_json(string_or_io : String | IO, root : String)
from_json(string_or_io : String | IO)
from_json
, from_yaml(string_or_io : String | IO) from_yaml

Macros inherited from class Object

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

Instance Method Detail

def cleanup=(cleanup : Bool) #

By default the compiler cleans up the default cache directory to keep the most recent 10 directories used. If this is set to false that cleanup is not performed.


[View source]
def cleanup? : Bool #

By default the compiler cleans up the default cache directory to keep the most recent 10 directories used. If this is set to false that cleanup is not performed.


[View source]
def codegen_target : Crystal::Codegen::Target #

Codegen target to use in the compilation. If not set, asks LLVM the default one for the current machine.


[View source]
def codegen_target=(codegen_target : Crystal::Codegen::Target) #

Codegen target to use in the compilation. If not set, asks LLVM the default one for the current machine.


[View source]
def color=(color : Bool) #

If false, color won't be used in output messages.


[View source]
def color? : Bool #

If false, color won't be used in output messages.


[View source]
def compile(source : Source | Array(Source), output_filename : String) : Result #

Compiles the given source, with output_filename as the name of the generated executable.

Raises Crystal::CodeError if there's an error in the source code.

Raises InvalidByteSequenceError if the source code is not valid UTF-8.


[View source]
def compile_configure_program(source : Source | Array(Source), output_filename : String, & : Program -> Nil) : Result #

Compiles the given source, with output_filename as the name of the generated executable.

Raises Crystal::CodeError if there's an error in the source code.

Raises InvalidByteSequenceError if the source code is not valid UTF-8.

Yields a Program instance before compiling.


[View source]
def create_target_machine #

[View source]
def cross_compile=(cross_compile : Bool) #

If true, doesn't generate an executable but instead creates a .o file and outputs a command line to link it in the target machine.


[View source]
def cross_compile? : Bool #

If true, doesn't generate an executable but instead creates a .o file and outputs a command line to link it in the target machine.


[View source]
def debug : Crystal::Debug #

If true, the executable will be generated with debug code that can be understood by gdb and lldb.


[View source]
def debug=(debug : Crystal::Debug) #

If true, the executable will be generated with debug code that can be understood by gdb and lldb.


[View source]
def dependency_printer : DependencyPrinter | Nil #

[View source]
def dependency_printer=(dependency_printer : DependencyPrinter | Nil) #

[View source]
def dump_ll=(dump_ll : Bool) #

If true, .ll files will be generated in the default cache directory for each generated LLVM module.


[View source]
def dump_ll? : Bool #

If true, .ll files will be generated in the default cache directory for each generated LLVM module.


[View source]
def emit_base_filename : String | Nil #

Base filename to use for emit output.


[View source]
def emit_base_filename=(emit_base_filename : String | Nil) #

Base filename to use for emit output.


[View source]
def emit_targets : EmitTarget #

Can be set to a set of flags to emit other files other than the executable file:

  • asm: assembly files
  • llvm-bc: LLVM bitcode
  • llvm-ir: LLVM IR
  • obj: object file

[View source]
def emit_targets=(emit_targets : EmitTarget) #

Can be set to a set of flags to emit other files other than the executable file:

  • asm: assembly files
  • llvm-bc: LLVM bitcode
  • llvm-ir: LLVM IR
  • obj: object file

[View source]
def flags : Array(String) #

Compiler flags. These will be true when checked in macro code by the flag?(...) macro method.


[View source]
def flags=(flags : Array(String)) #

Compiler flags. These will be true when checked in macro code by the flag?(...) macro method.


[View source]
def frame_pointers : Crystal::FramePointers #

Controls generation of frame pointers.


[View source]
def frame_pointers=(frame_pointers : Crystal::FramePointers) #

Controls generation of frame pointers.


[View source]
def link_flags : String | Nil #

Additional link flags to pass to the linker.


[View source]
def link_flags=(link_flags : String | Nil) #

Additional link flags to pass to the linker.


[View source]
def mattr : String | Nil #

Sets the mattr (features). Check LLVM docs to learn about this.


[View source]
def mattr=(mattr : String | Nil) #

Sets the mattr (features). Check LLVM docs to learn about this.


[View source]
def mcmodel : LLVM::CodeModel #

Sets the code model. Check LLVM docs to learn about this.


[View source]
def mcmodel=(mcmodel : LLVM::CodeModel) #

Sets the code model. Check LLVM docs to learn about this.


[View source]
def mcpu : String | Nil #

Sets the mcpu. Check LLVM docs to learn about this.


[View source]
def mcpu=(mcpu : String | Nil) #

Sets the mcpu. Check LLVM docs to learn about this.


[View source]
def n_threads : Int32 #

Maximum number of LLVM modules that are compiled in parallel


[View source]
def n_threads=(n_threads : Int32) #

Maximum number of LLVM modules that are compiled in parallel


[View source]
def no_cleanup=(no_cleanup : Bool) #

If true, skip cleanup process on semantic analysis.


[View source]
def no_cleanup? : Bool #

If true, skip cleanup process on semantic analysis.


[View source]
def no_codegen=(no_codegen : Bool) #

If true, no executable will be generated after compilation (useful to type-check a program)


[View source]
def no_codegen? : Bool #

If true, no executable will be generated after compilation (useful to type-check a program)


[View source]
def optimization_mode : Crystal::Compiler::OptimizationMode #

Sets the Optimization mode.


[View source]
def optimization_mode=(optimization_mode : Crystal::Compiler::OptimizationMode) #

Sets the Optimization mode.


[View source]
def prelude : String #

Default prelude file to use. This ends up adding a require "prelude" (or whatever name is set here) to the source file to compile.


[View source]
def prelude=(prelude : String) #

Default prelude file to use. This ends up adding a require "prelude" (or whatever name is set here) to the source file to compile.


[View source]
def program : Program #

Program that was created for the last compilation.


[View source]
def program=(program : Program) #

Program that was created for the last compilation.


[View source]
def program? : Program | Nil #

Program that was created for the last compilation.


[View source]
def progress_tracker : Crystal::ProgressTracker #

A ProgressTracker object which tracks compilation progress.


[View source]
def progress_tracker=(progress_tracker : Crystal::ProgressTracker) #

A ProgressTracker object which tracks compilation progress.


[View source]
def release! #

Set maximum level of optimization.


[View source]
def release? #

[View source]
def show_error_trace=(show_error_trace : Bool) #

Whether to show error trace


[View source]
def show_error_trace? : Bool #

Whether to show error trace


[View source]
def single_module=(single_module : Bool) #

If true, generates a single LLVM module. By default one LLVM module is created for each type in a program. --release automatically enable this option


[View source]
def single_module? : Bool #

If true, generates a single LLVM module. By default one LLVM module is created for each type in a program. --release automatically enable this option


[View source]
def static=(static : Bool) #

Whether to link statically


[View source]
def static? : Bool #

Whether to link statically


[View source]
def stderr : IO #

Default standard error to use in a compilation.


[View source]
def stderr=(stderr : IO) #

Default standard error to use in a compilation.


[View source]
def stdout : IO #

Default standard output to use in a compilation.


[View source]
def stdout=(stdout : IO) #

Default standard output to use in a compilation.


[View source]
def target_machine : LLVM::TargetMachine #

[View source]
def top_level_semantic(source : Source | Array(Source)) : Result #

Runs the semantic pass on the given source, without generating an executable nor analyzing methods. The returned Program in the result will contain all types and methods. This can be useful to generate API docs, analyze type relationships, etc.

Raises Crystal::CodeError if there's an error in the source code.

Raises InvalidByteSequenceError if the source code is not valid UTF-8.


[View source]
def verbose=(verbose : Bool) #

If true, prints the link command line that is performed to create the executable.


[View source]
def verbose? : Bool #

If true, prints the link command line that is performed to create the executable.


[View source]
def wants_doc=(wants_doc : Bool) #

If true, doc comments are attached to types and methods and can later be used to generate API docs.


[View source]
def wants_doc? : Bool #

If true, doc comments are attached to types and methods and can later be used to generate API docs.


[View source]
def warnings : Crystal::WarningCollection #

Warning settings and all detected warnings.


[View source]
def warnings=(warnings : Crystal::WarningCollection) #

Warning settings and all detected warnings.


[View source]