Skip to content

Commit

Permalink
Showing 16 changed files with 409 additions and 269 deletions.
8 changes: 6 additions & 2 deletions spec/compiler/codegen/magic_constants_spec.cr
Original file line number Diff line number Diff line change
@@ -58,22 +58,26 @@ describe "Code gen: magic constants" do

it "does __LINE__ when specifying one normal default arg" do
run(%(
require "primitives"
def foo(x, z = 10, line = __LINE__)
z + line
end
foo 1, 20
), inject_primitives: false).to_i.should eq(26)
), inject_primitives: false).to_i.should eq(28)
end

it "does __LINE__ when specifying one middle argument" do
run(%(
require "primitives"
def foo(x, line = __LINE__, z = 1)
z + line
end
foo 1, z: 20
), inject_primitives: false).to_i.should eq(26)
), inject_primitives: false).to_i.should eq(28)
end

it "does __LINE__ in macro" do
4 changes: 2 additions & 2 deletions spec/compiler/type_inference/cleanup_spec.cr
Original file line number Diff line number Diff line change
@@ -47,8 +47,8 @@ describe "cleanup" do
end

it "keeps then of if with responds_to? that is always true" do
assert_after_cleanup "a = 1; if a.responds_to?(:\"+\"); 2; end",
"a = 1\n2"
assert_after_cleanup "struct Int; def +(other); end; end; a = 1; if a.responds_to?(:\"+\"); 2; end",
"struct Int\n def +(other)\n end\nend\na = 1\n2"
end

it "errors if assigning var to itself" do
12 changes: 11 additions & 1 deletion spec/compiler/type_inference/def_spec.cr
Original file line number Diff line number Diff line change
@@ -90,7 +90,17 @@ describe "Type inference: def" do
end

it "do not use body for the def type" do
input = parse "def foo; if 1 == 2; return 0; end; end; foo"
input = parse %(
require "primitives"
def foo
if 1 == 2
return 0
end
end
foo
)
result = infer_type input
mod, input = result.program, result.node as Expressions

6 changes: 4 additions & 2 deletions spec/compiler/type_inference/responds_to_spec.cr
Original file line number Diff line number Diff line change
@@ -6,12 +6,14 @@ describe "Type inference: responds_to?" do
end

it "restricts type inside if scope 1" do
nodes = parse "
nodes = parse %(
require "primitives"
a = 1 || 'a'
if a.responds_to?(:\"+\")
a
end
"
)
result = infer_type nodes
mod, nodes = result.program, result.node as Expressions
(nodes.last as If).then.type.should eq(mod.int32)
12 changes: 8 additions & 4 deletions spec/compiler/type_inference/yield_with_scope_spec.cr
Original file line number Diff line number Diff line change
@@ -26,26 +26,30 @@ describe "Type inference: yield with scope" do
end

it "infer type of block body with yield scope" do
input = parse "
input = parse %(
require "primitives"
def foo; with 1 yield; end
foo do
to_i64
end
"
)
result = infer_type input
mod, input = result.program, result.node as Expressions
(input.last as Call).block.not_nil!.body.type.should eq(mod.int64)
end

it "infer type of block body with yield scope and arguments" do
input = parse "
input = parse %(
require "primitives"
def foo; with 1 yield 1.5; end
foo do |f|
to_i64 + f
end
"
)
result = infer_type input
mod, input = result.program, result.node as Expressions
(input.last as Call).block.not_nil!.body.type.should eq(mod.float64)
22 changes: 22 additions & 0 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
@@ -1908,6 +1908,28 @@ module Crystal
name
end
end

class Program
def sprintf(llvm_mod)
llvm_mod.functions["sprintf"]? || llvm_mod.functions.add("sprintf", [LLVM::VoidPointer], LLVM::Int32, true)
end

def printf(llvm_mod)
llvm_mod.functions["printf"]? || llvm_mod.functions.add("printf", [LLVM::VoidPointer], LLVM::Int32, true)
end

def realloc(llvm_mod)
llvm_mod.functions["realloc"]? || llvm_mod.functions.add("realloc", ([LLVM::VoidPointer, LLVM::Int64]), LLVM::VoidPointer)
end

def memset(llvm_mod)
llvm_mod.functions["llvm.memset.p0i8.i32"]? || llvm_mod.functions.add("llvm.memset.p0i8.i32", [LLVM::VoidPointer, LLVM::Int8, LLVM::Int32, LLVM::Int32, LLVM::Int1], LLVM::Void)
end

def memcpy(llvm_mod)
llvm_mod.functions["llvm.memcpy.p0i8.p0i8.i32"]? || llvm_mod.functions.add("llvm.memcpy.p0i8.p0i8.i32", [LLVM::VoidPointer, LLVM::VoidPointer, LLVM::Int32, LLVM::Int32, LLVM::Int1], LLVM::Void)
end
end
end

require "./*"
6 changes: 0 additions & 6 deletions src/compiler/crystal/codegen/primitives.cr
Original file line number Diff line number Diff line change
@@ -57,8 +57,6 @@ class Crystal::CodeGenVisitor
codegen_primitive_object_id node, target_def, call_args
when "object_crystal_type_id"
codegen_primitive_object_crystal_type_id node, target_def, call_args
when "symbol_hash"
codegen_primitive_symbol_hash node, target_def, call_args
when "symbol_to_s"
codegen_primitive_symbol_to_s node, target_def, call_args
when "class"
@@ -593,10 +591,6 @@ class Crystal::CodeGenVisitor
load(gep @llvm_mod.globals[SYMBOL_TABLE_NAME], int(0), call_args[0])
end

def codegen_primitive_symbol_hash(node, target_def, call_args)
call_args[0]
end

def codegen_primitive_class(node, target_def, call_args)
value = call_args.first?
if value
119 changes: 0 additions & 119 deletions src/compiler/crystal/primitives.cr

This file was deleted.

2 changes: 0 additions & 2 deletions src/compiler/crystal/program.cr
Original file line number Diff line number Diff line change
@@ -169,8 +169,6 @@ module Crystal
@literal_expander = LiteralExpander.new self
@macro_expander = MacroExpander.new self
@nil_var = Var.new("<nil_var>", nil_t)

define_primitives
end

private def crystal_path
1 change: 0 additions & 1 deletion src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "levenshtein"
require "../syntax/ast"
require "../types"
require "../primitives"
require "./type_lookup"

class Crystal::Call
2 changes: 0 additions & 2 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
@@ -1878,8 +1878,6 @@ module Crystal
node.type = mod.uint64
when "object_crystal_type_id"
node.type = mod.int32
when "symbol_hash"
node.type = mod.int32
when "symbol_to_s"
node.type = mod.string
when "class"
7 changes: 0 additions & 7 deletions src/compiler/crystal/tools/doc/generator.cr
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ class Crystal::Doc::Generator
def initialize(@program : Program, @included_dirs : Array(String), @dir = "./doc")
@base_dir = `pwd`.chomp
@types = {} of Crystal::Type => Doc::Type
@is_crystal_repository = false
@repo_name = ""
compute_repository
end
@@ -114,10 +113,6 @@ class Crystal::Doc::Generator
end

def must_include?(a_def : Crystal::Def)
if @is_crystal_repository && (body = a_def.body).is_a?(Crystal::Primitive)
doc = Primitive.doc(a_def, body)
return !nodoc?(doc)
end
return false if nodoc?(a_def)

must_include? a_def.location
@@ -253,8 +248,6 @@ class Crystal::Doc::Generator

@repository = "https://github.com/#{user}/#{repo}/blob/#{rev}"
@repo_name = "github.com/#{user}/#{repo}"

@is_crystal_repository ||= (user == "crystal-lang" && repo == "crystal")
end

def source_link(node)
8 changes: 1 addition & 7 deletions src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
@@ -20,13 +20,7 @@ class Crystal::Doc::Method
end

def doc
body = @def.body
doc = @def.doc
if !doc && body.is_a?(Crystal::Primitive)
Primitive.doc @def, body
else
doc
end
@def.doc
end

def source_link
Loading

0 comments on commit e9a73cb

Please sign in to comment.