Class: RLTK::CG::Module::GlobalCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rltk/cg/module.rb

Overview

This class is used to access a module’s global variables.

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ GlobalCollection

Returns a new instance of GlobalCollection.

Parameters:

  • mod (Module)

    Module for which this is a proxy.



318
319
320
# File 'lib/rltk/cg/module.rb', line 318

def initialize(mod)
	@module = mod
end

Instance Method Details

#[](key) ⇒ GlobalVariable

Retreive a GlobalVariable object.

Parameters:

  • key (String, Symbol, Integer)

    Global variable identifier. Either the name of the variable or its index.

Returns:



327
328
329
330
331
332
333
334
335
# File 'lib/rltk/cg/module.rb', line 327

def [](key)
	case key
	when String, Symbol
		self.named(key)
		
	when Integer
		(1...key).inject(self.first) { |global| if global then self.next(global) else break end }
	end
end

#add(type, name) ⇒ Object

Add a global variable to a module.

Parameters:

  • type (Type)

    Type of the global variable.

  • name (String)

    Name of the global variable in LLVM IR.



341
342
343
# File 'lib/rltk/cg/module.rb', line 341

def add(type, name)
	GlobalVariable.new(Bindings.add_global(@module, type, name))
end

#delete(global) ⇒ void

This method returns an undefined value.

Remove a global variable from the module.

Parameters:



350
351
352
# File 'lib/rltk/cg/module.rb', line 350

def delete(global)
	Bindings.delete_global(global)
end

#each {|fun| ... } ⇒ Enumerator

An iterator for each global variable inside this collection.

Yield Parameters:

Returns:

  • (Enumerator)

    Returns an Enumerator if no block is given.



359
360
361
362
363
364
365
366
367
368
# File 'lib/rltk/cg/module.rb', line 359

def each
	return to_enum(:each) unless block_given?
	
	global = self.first
	
	while global
		yield global
		global = self.next(global)
	end
end

#firstGlobalVariable?

Returns The module’s first global variable if one has been added.

Returns:

  • (GlobalVariable, nil)

    The module’s first global variable if one has been added.



371
372
373
# File 'lib/rltk/cg/module.rb', line 371

def first
	if (ptr = Bindings.get_first_global(@module)).null? then nil else GlobalValue.new(ptr) end
end

#lastGlobalVariable?

Returns The module’s last global variable if one has been added.

Returns:

  • (GlobalVariable, nil)

    The module’s last global variable if one has been added.



376
377
378
# File 'lib/rltk/cg/module.rb', line 376

def last
	if (ptr = Bindings.get_last_global(@module)).null? then nil else GlobalValue.new(ptr) end
end

#named(name) ⇒ GlobalVariable?

Returns The global variable with the given name.

Parameters:

  • name (String, Symbol)

    Name of the desired global variable.

Returns:



383
384
385
# File 'lib/rltk/cg/module.rb', line 383

def named(name)
	if (ptr = Bindings.get_named_global(@module, name)).null? then nil else GlobalValue.new(ptr) end
end

#next(global) ⇒ GlobalVariable?

Returns global Next global variable in the collection.

Parameters:

  • global (GlobalVariable)

    Global variable you want the successor for.

Returns:

  • (GlobalVariable, nil)

    global Next global variable in the collection.



390
391
392
# File 'lib/rltk/cg/module.rb', line 390

def next(global)
	if (ptr = Bindings.get_next_global(global)).null? then nil else GlobalValue.new(ptr) end
end

#previous(global) ⇒ GlobalVariable?

Returns Previous global variable in the collection.

Parameters:

  • global (GlobalVariable)

    Global variable you want the predecessor for.

Returns:

  • (GlobalVariable, nil)

    Previous global variable in the collection.



397
398
399
# File 'lib/rltk/cg/module.rb', line 397

def previous(global)
	if (ptr = Bindings.get_previous_global(global)).null? then nil else GlobalValue.new(ptr) end
end