Skip to content

Commit

Permalink
Passthrough docs for c struct vars
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere committed Jul 17, 2024
1 parent 2ac8581 commit 576058e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/compiler/crystal/semantic/type_declaration_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,25 @@ class Crystal::TypeDeclarationVisitor < Crystal::SemanticVisitor
if type.lookup_instance_var?(var_name)
node.raise "#{type.type_desc} #{type} already defines a field named '#{field_name}'"
end

ivar = MetaTypeVar.new(var_name, field_type)
ivar.doc = node.var.as(Var).doc
ivar.owner = type

declare_c_struct_or_union_field(type, field_name, ivar, node.location)
end

def declare_c_struct_or_union_field(type, field_name, var, location)
type.instance_vars[var.name] = var
type.add_def Def.new("#{field_name}=", [Arg.new("value")], Primitive.new("struct_or_union_set").at(location)).at(location)
type.add_def Def.new(field_name, body: InstanceVar.new(var.name)).at(location)

setter = Def.new("#{field_name}=", [Arg.new("value")], Primitive.new("struct_or_union_set").at(location)).at(location)
setter.doc = var.doc

getter = Def.new(field_name, body: InstanceVar.new(var.name)).at(location)
getter.doc = var.doc

type.add_def setter
type.add_def getter
end

def declare_instance_var(node, var)
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ module Crystal
include SpecialVar

property name : String
property doc : String?

def initialize(@name : String)
end
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6023,6 +6023,7 @@ module Crystal
end

def parse_c_struct_or_union_fields(exps)
doc = @token.doc
vars = [Var.new(@token.value.to_s).at(@token.location).at_end(token_end_location)]

next_token_skip_space_or_newline
Expand All @@ -6041,6 +6042,7 @@ module Crystal
skip_statement_end

vars.each do |var|
var.doc = doc
exps << TypeDeclaration.new(var, type).at(var).at_end(type)
end
end
Expand Down

0 comments on commit 576058e

Please sign in to comment.