Class: Rlang::Parser::Attr

Inherits:
Object show all
Includes:
Log
Defined in:
lib/rlang/parser/attr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

included, logger, #logger, logger=

Constructor Details

#initialize(klass, name, wtype = WType::DEFAULT) ⇒ Attr

The name argument can either be the attribute name (e.g. :size) or an ivar name (e.g. :@size)



18
19
20
21
22
23
24
25
26
# File 'lib/rlang/parser/attr.rb', line 18

def initialize(klass, name, wtype=WType::DEFAULT)
  @klass = klass
  @name = name
  @ivar = klass.wnode.create_ivar(:"@#{name}", wtype)
  @getter = nil
  @setter = nil
  @export = false
  logger.debug "Class attribute #{name} created"
end

Instance Attribute Details

#getterObject (readonly)

Returns the value of attribute getter.



14
15
16
# File 'lib/rlang/parser/attr.rb', line 14

def getter
  @getter
end

#ivarObject (readonly)

Returns the value of attribute ivar.



14
15
16
# File 'lib/rlang/parser/attr.rb', line 14

def ivar
  @ivar
end

#klassObject (readonly)

Returns the value of attribute klass.



14
15
16
# File 'lib/rlang/parser/attr.rb', line 14

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/rlang/parser/attr.rb', line 14

def name
  @name
end

#setterObject (readonly)

Returns the value of attribute setter.



14
15
16
# File 'lib/rlang/parser/attr.rb', line 14

def setter
  @setter
end

Instance Method Details

#attr_accessorObject



42
43
44
# File 'lib/rlang/parser/attr.rb', line 42

def attr_accessor
  [self.attr_reader, self.attr_writer]
end

#attr_readerObject



28
29
30
31
32
33
# File 'lib/rlang/parser/attr.rb', line 28

def attr_reader
  @getter = @klass.wnode.find_or_create_method(nil, self.getter_name, :instance, wtype)
  @getter.export! if @export
  logger.debug "Getter created: #{@getter}"
  @getter
end

#attr_writerObject



35
36
37
38
39
40
# File 'lib/rlang/parser/attr.rb', line 35

def attr_writer
  @setter = @klass.wnode.find_or_create_method(nil, self.setter_name, :instance, wtype)
  @setter.export! if @export
  logger.debug "Setter created: #{@setter}"
  @setter
end

#export!Object



46
47
48
# File 'lib/rlang/parser/attr.rb', line 46

def export!
  @export = true
end

#getter_nameObject



66
67
68
# File 'lib/rlang/parser/attr.rb', line 66

def getter_name
  @name
end

#offsetObject



54
55
56
# File 'lib/rlang/parser/attr.rb', line 54

def offset
  @ivar.offset
end

#setter_nameObject



70
71
72
# File 'lib/rlang/parser/attr.rb', line 70

def setter_name
  "#{@name}=".to_sym
end

#wasm_nameObject



74
75
76
# File 'lib/rlang/parser/attr.rb', line 74

def wasm_name
  "$#{@name}"
end

#wasm_typeObject



78
79
80
# File 'lib/rlang/parser/attr.rb', line 78

def wasm_type
  self.wtype.wasm_type
end

#wtypeObject



50
51
52
# File 'lib/rlang/parser/attr.rb', line 50

def wtype
  @ivar.wtype
end

#wtype=(wtype) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rlang/parser/attr.rb', line 58

def wtype=(wtype)
  # Adjust getter/setter and ivar wtype accordingly
  @getter.wtype = wtype if @getter
  @setter.wtype = wtype if @setter
  @ivar.wtype = wtype
  logger.debug "Attr/Getter/Setter/ivar wtype updated : #{@getter}"
end