Class: Citrus::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/citrus/compiler/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args, mod) ⇒ Function

Returns a new instance of Function.



7
8
9
10
11
12
13
14
15
16
# File 'lib/citrus/compiler/function.rb', line 7

def initialize(name, args, mod)
  @name = name
  @module = mod
  @rtype = LLVM::Type.opaque
  @atypes = args.map{LLVM::Type.opaque}
  @args = args.map{|arg| arg.class.to_s == "String" ? arg.to_a : arg}
  @varargs = @args.empty? ? false : (@args.last[0].slice(0, 1) == "*")
  @func = @module.functions.add(name, LLVM::Type.function(@atypes, @rtype, :varargs => @vaargs))
  build_function { |g| yield g if block_given? }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



45
46
47
# File 'lib/citrus/compiler/function.rb', line 45

def method_missing(symbol, *args, &block)
  @func.send(symbol, *args, &block)
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator.



5
6
7
# File 'lib/citrus/compiler/function.rb', line 5

def generator
  @generator
end

#varargsObject (readonly)

Returns the value of attribute varargs.



4
5
6
# File 'lib/citrus/compiler/function.rb', line 4

def varargs
  @varargs
end

Instance Method Details

#argsObject



18
19
20
# File 'lib/citrus/compiler/function.rb', line 18

def args
  return @args.map{|arg| arg[0]}
end

#call(args, builder) ⇒ Object



27
28
29
30
31
32
# File 'lib/citrus/compiler/function.rb', line 27

def call(args, builder)
  for i in 0...@atypes.size
    @atypes[i].refine(LLVM::Type(args[i])) if @atypes[i].kind == :opaque
  end
  builder.call(@func, *args)
end

#force_types(args, ret = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/citrus/compiler/function.rb', line 34

def force_types(args, ret=nil)
  unless args.empty?
    for i in 0...@atypes.size
      @atypes[i].refine(LLVM::Type(args[i])) if @atypes[i].kind == :opaque
    end
  end
  unless ret.nil?
    @rtype.refine(ret) if @rtype.kind == :opaque
  end
end

#return(value, builder) ⇒ Object



22
23
24
25
# File 'lib/citrus/compiler/function.rb', line 22

def return(value, builder)
  @rtype.refine(LLVM::Type(value)) if @rtype.kind == :opaque
  builder.ret(value)
end