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.



394
395
396
397
398
399
400
# File 'lib/dbc/ctype.rb', line 394

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.



416
417
418
# File 'lib/dbc/ctype.rb', line 416

def dimensions
  @dimensions
end

#identifierObject (readonly)

Returns the value of attribute identifier.



416
417
418
# File 'lib/dbc/ctype.rb', line 416

def identifier
  @identifier
end

#parametersObject

Returns the value of attribute parameters.



416
417
418
# File 'lib/dbc/ctype.rb', line 416

def parameters
  @parameters
end

#sizeObject

Returns the value of attribute size.



416
417
418
# File 'lib/dbc/ctype.rb', line 416

def size
  @size
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


413
414
415
# File 'lib/dbc/ctype.rb', line 413

def abstract?
	@identifier == nil
end

#add_declaration_specifiers(val) ⇒ Object

array of specifiers, last element is type specifier



440
441
442
443
444
445
446
447
448
# File 'lib/dbc/ctype.rb', line 440

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



424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/dbc/ctype.rb', line 424

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)


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

def array?
	@dimensions.length > 0
end

#bit_field?Boolean

Returns:

  • (Boolean)


462
463
464
# File 'lib/dbc/ctype.rb', line 462

def bit_field?
	@size != nil
end

#dupObject



402
403
404
405
406
407
408
409
410
411
# File 'lib/dbc/ctype.rb', line 402

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



466
467
468
469
470
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
# File 'lib/dbc/ctype.rb', line 466

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)


500
501
502
# File 'lib/dbc/ctype.rb', line 500

def function?
	@parameters != nil
end

#prototype?Boolean

Returns:

  • (Boolean)


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

def prototype?
	@parameters.prototype?
end

#set_typedefObject

override this

Raises:

  • (ParseError)


451
452
453
454
455
# File 'lib/dbc/ctype.rb', line 451

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

#short_nameObject



534
535
536
537
538
539
540
541
542
# File 'lib/dbc/ctype.rb', line 534

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

#to_init_s(ident = nil) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/dbc/ctype.rb', line 512

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



530
531
532
# File 'lib/dbc/ctype.rb', line 530

def to_s
	to_init_s
end