Class: CType::Type

Inherits:
Object
  • Object
show all
Includes:
BaseReference, Specifiers
Defined in:
lib/dbc/ctype.rb

Overview

Pointer

Instance Attribute Summary collapse

Attributes included from BaseReference

#base_type

Instance Method Summary collapse

Methods included from Specifiers

#add_specifier

Constructor Details

#initialize(identifier = nil) ⇒ Type

Returns a new instance of Type.



399
400
401
402
403
404
405
# File 'lib/dbc/ctype.rb', line 399

def initialize(identifier=nil)
	@identifier = identifier
	@dimensions = []
	@parameters = nil
	@base_type = nil
	@size = nil
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



421
422
423
# File 'lib/dbc/ctype.rb', line 421

def dimensions
  @dimensions
end

#identifierObject (readonly)

Returns the value of attribute identifier.



421
422
423
# File 'lib/dbc/ctype.rb', line 421

def identifier
  @identifier
end

#parametersObject

Returns the value of attribute parameters.



421
422
423
# File 'lib/dbc/ctype.rb', line 421

def parameters
  @parameters
end

#sizeObject

Returns the value of attribute size.



421
422
423
# File 'lib/dbc/ctype.rb', line 421

def size
  @size
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


418
419
420
# File 'lib/dbc/ctype.rb', line 418

def abstract?
	@identifier == nil
end

#add_declaration_specifiers(val) ⇒ Object

array of specifiers, last element is type specifier



445
446
447
448
449
450
451
452
453
# File 'lib/dbc/ctype.rb', line 445

def add_declaration_specifiers(val)
	# make sure we don't modify specifiers
	val = val.dup
	self.base_type = val.pop
	val.each do |s|
		self.add_specifier(s)
	end
	self
end

#add_parameter_declarations(declarations) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/dbc/ctype.rb', line 429

def add_parameter_declarations(declarations)
	# @parameters should be an identifier list
	if @parameters.length != declarations.length
		raise ParseError, "invalid function definition"
	end
	declarations.each do |d|
		idx = @parameters.index(d.identifier)
		unless idx
			raise ParseError, "identifier not found in parameter list: #{d.identifier}"
		end
		@parameters[idx] = d
	end
	self
end

#array?Boolean

Returns:

  • (Boolean)


509
510
511
# File 'lib/dbc/ctype.rb', line 509

def array?
	@dimensions.length > 0
end

#bit_field?Boolean

Returns:

  • (Boolean)


467
468
469
# File 'lib/dbc/ctype.rb', line 467

def bit_field?
	@size != nil
end

#dupObject



407
408
409
410
411
412
413
414
415
416
# File 'lib/dbc/ctype.rb', line 407

def dup
	new_type = Type.new(self.identifier)
	self.dimensions.each do |d|
		new_type.dimensions << d
	end
	new_type.parameters = self.parameters
	new_type.base_type = self.base_type
	new_type.size = self.size
	new_type
end

#evaluate(identifier) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/dbc/ctype.rb', line 471

def evaluate(identifier)
	if identifier.empty?
		result = self
	elsif identifier =~ /\A[A-Za-z_]\w*/
		post_match = $'
		p_type = @parameters.lookup($&) if function?
		CType.evaluation_error(identifier) unless p_type
		p_type.evaluate(post_match)
	elsif identifier =~ /\A(?:\s*\[[^\]]*\])+/
		if self.dimensions.empty?
			# then let the base type evaluate this
			@base_type.evaluate(identifier)
		else
			post_match = $'
			
			indicies = $&.scan(/\[.+?\]/)
			ret_type = self.dup
	
			min = indicies.length
			if min > self.dimensions.length
				min = self.dimensions.length
			end
			# remove lower dimensions
			min.times do
				ret_type.dimensions.shift
				indicies.shift
			end
			ret_type.evaluate(indicies.join << post_match)
		end
	else
		@base_type.evaluate(identifier)
	end
end

#function?Boolean

Returns:

  • (Boolean)


505
506
507
# File 'lib/dbc/ctype.rb', line 505

def function?
	@parameters != nil
end

#prototype?Boolean

Returns:

  • (Boolean)


513
514
515
# File 'lib/dbc/ctype.rb', line 513

def prototype?
	@parameters.prototype?
end

#set_typedefObject

override this

Raises:

  • (ParseError)


456
457
458
459
460
# File 'lib/dbc/ctype.rb', line 456

def set_typedef
	raise ParseError, "typedef must have a name" if abstract?
	@typedef = true
	self
end

#short_nameObject



539
540
541
542
543
544
545
546
547
# File 'lib/dbc/ctype.rb', line 539

def short_name
	if typedef?
		@identifier.dup
	elsif function?
		"#{@identifier}()"
	else
		to_init_s('')
	end
end

#to_init_s(ident = nil) ⇒ Object



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/dbc/ctype.rb', line 517

def to_init_s(ident=nil)
	str = ''
	if typedef?
		str << @identifier
		str << ' ' << ident if ident
	else
		ident = @identifier unless ident
		str << ident if ident
		str << '(' << @parameters.to_s << ')' if @parameters
		@dimensions.each do |d|
			str << "[#{d}]"
		end
		# should pass nil to base_type if we have nothing to add
		str = @base_type.to_init_s(str.empty? ? nil : str) if @base_type
	end
	str
end

#to_sObject



535
536
537
# File 'lib/dbc/ctype.rb', line 535

def to_s
	to_init_s
end