Class: Rubycc::Type::Array

Inherits:
Data
  • Object
show all
Defined in:
lib/rubycc/type.rb

Overview

A one-dimensional array of length elements, each of type element (itself a Type: an int or a pointer in this subset). Two arrays are equal when both their element type and length match.

A length of nil is an incomplete array type (6.7.2.1): an unbounded "[]" whose element count is unknown. It reaches this subset as a struct's flexible array member (the last member, "T name;", ISO C 6.7.2.1p18) and nowhere a size is needed — #size raises, and every path that could demand one (a variable, a plain sizeof/_Alignof of the array, an array-of-array element) is rejected first by the parser or the generator's completeness guard. A flexible array member still lays out (at its element's boundary, contributing nothing to the struct's size), and an lvalue of this type decays to a pointer to its element exactly as a bounded array does, so "p->fam" indexes it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element

Returns:

  • (Object)

    the current value of element



431
432
433
# File 'lib/rubycc/type.rb', line 431

def element
  @element
end

#lengthObject (readonly)

Returns the value of attribute length

Returns:

  • (Object)

    the current value of length



431
432
433
# File 'lib/rubycc/type.rb', line 431

def length
  @length
end

Instance Method Details

#alignmentObject

An array is aligned like one of its elements: an int [10] on a 4-byte boundary, a struct point [3] on the struct's boundary.



495
496
497
# File 'lib/rubycc/type.rb', line 495

def alignment
  element.alignment
end

#arithmetic?Boolean

Returns:

  • (Boolean)


452
453
454
# File 'lib/rubycc/type.rb', line 452

def arithmetic?
  false
end

#array?Boolean

Returns:

  • (Boolean)


460
461
462
# File 'lib/rubycc/type.rb', line 460

def array?
  true
end

#bool?Boolean

Returns:

  • (Boolean)


456
457
458
# File 'lib/rubycc/type.rb', line 456

def bool?
  false
end

#char?Boolean

Returns:

  • (Boolean)


440
441
442
# File 'lib/rubycc/type.rb', line 440

def char?
  false
end

#float?Boolean

Returns:

  • (Boolean)


472
473
474
# File 'lib/rubycc/type.rb', line 472

def float?
  false
end

#function?Boolean

Returns:

  • (Boolean)


468
469
470
# File 'lib/rubycc/type.rb', line 468

def function?
  false
end

#incomplete?Boolean

Whether this is an incomplete array — an unbounded "[]" with no element count, the shape a struct's flexible array member takes. It has no size, so every size-needing use is diagnosed before #size would raise.

Returns:

  • (Boolean)


479
480
481
# File 'lib/rubycc/type.rb', line 479

def incomplete?
  length.nil?
end

#int?Boolean

Returns:

  • (Boolean)


436
437
438
# File 'lib/rubycc/type.rb', line 436

def int?
  false
end

#integer?Boolean

Returns:

  • (Boolean)


448
449
450
# File 'lib/rubycc/type.rb', line 448

def integer?
  false
end

#pointer?Boolean

Returns:

  • (Boolean)


432
433
434
# File 'lib/rubycc/type.rb', line 432

def pointer?
  false
end

#sizeObject

The whole array's byte size: the element width times the count. An incomplete array ("[]") has no count and therefore no size; every path that could reach one where a size is needed rejects it first with a proper diagnostic, so a raise here is a missing guard.



487
488
489
490
491
# File 'lib/rubycc/type.rb', line 487

def size
  raise "incomplete array has no size" if length.nil?

  element.size * length
end

#struct?Boolean

Returns:

  • (Boolean)


464
465
466
# File 'lib/rubycc/type.rb', line 464

def struct?
  false
end

#to_sObject

Renders like a C array declarator: the element type, a space, then the bracketed length ("int [10]", "int * [4]"); an incomplete array shows an empty "[]" ("int []").



502
503
504
# File 'lib/rubycc/type.rb', line 502

def to_s
  "#{element} [#{length}]"
end

#void?Boolean

Returns:

  • (Boolean)


444
445
446
# File 'lib/rubycc/type.rb', line 444

def void?
  false
end