Class: CType::Type

Inherits:
Object
  • Object
show all
Includes:
BaseReference, Specifiers
Defined in:
lib/caphir/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.


402
403
404
405
406
407
408
# File 'lib/caphir/ctype.rb', line 402

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


424
425
426
# File 'lib/caphir/ctype.rb', line 424

def dimensions
  @dimensions
end

#identifierObject (readonly)

Returns the value of attribute identifier


424
425
426
# File 'lib/caphir/ctype.rb', line 424

def identifier
  @identifier
end

#parametersObject

Returns the value of attribute parameters


424
425
426
# File 'lib/caphir/ctype.rb', line 424

def parameters
  @parameters
end

#sizeObject

Returns the value of attribute size


424
425
426
# File 'lib/caphir/ctype.rb', line 424

def size
  @size
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)

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

def abstract?
	@identifier == nil
end

#add_declaration_specifiers(val) ⇒ Object

array of specifiers, last element is type specifier


448
449
450
451
452
453
454
455
# File 'lib/caphir/ctype.rb', line 448

def add_declaration_specifiers(val)
	# make sure we don't modify specifiers
	self.base_type = val.last
	val[0...-1].each do |s|
		self.add_specifier(s)
	end
	self
end

#add_parameter_declarations(declarations) ⇒ Object


432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/caphir/ctype.rb', line 432

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)

511
512
513
# File 'lib/caphir/ctype.rb', line 511

def array?
	@dimensions.length > 0
end

#bit_field?Boolean

Returns:

  • (Boolean)

469
470
471
# File 'lib/caphir/ctype.rb', line 469

def bit_field?
	@size != nil
end

#dupObject


410
411
412
413
414
415
416
417
418
419
# File 'lib/caphir/ctype.rb', line 410

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


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
504
505
# File 'lib/caphir/ctype.rb', line 473

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)

507
508
509
# File 'lib/caphir/ctype.rb', line 507

def function?
	@parameters != nil
end

#prototype?Boolean

Returns:

  • (Boolean)

515
516
517
# File 'lib/caphir/ctype.rb', line 515

def prototype?
	@parameters.prototype?
end

#set_typedefObject

override this

Raises:

  • (ParseError)

458
459
460
461
462
# File 'lib/caphir/ctype.rb', line 458

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

#short_nameObject


541
542
543
544
545
546
547
548
549
# File 'lib/caphir/ctype.rb', line 541

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

#to_init_s(ident = nil) ⇒ Object


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

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


537
538
539
# File 'lib/caphir/ctype.rb', line 537

def to_s
	to_init_s
end