1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ui: Objective-C @interface support for Cocoa; minor clean-ups

This commit is contained in:
Alexander Medvednikov
2019-09-21 21:38:12 +03:00
parent fbc19311b5
commit bf1ee28194
11 changed files with 112 additions and 89 deletions

View File

@ -63,16 +63,6 @@ fn (p &Parser) find_var_check_new_var(name string) ?Var {
return none
}
fn (f &Fn) find_var2(name string) Var {
for i in 0 .. f.var_idx {
if f.local_vars[i].name == name {
return f.local_vars[i]
}
}
return Var{}
}
fn (p mut Parser) open_scope() {
p.cur_fn.defer_text << ''
p.cur_fn.scope_level++
@ -736,7 +726,7 @@ fn (p mut Parser) fn_args(f mut Fn) {
is_arg: true
// is_mut: is_mut
line_nr: p.scanner.line_nr
scanner_pos: p.scanner.get_scanner_pos()
scanner_pos: p.scanner.get_scanner_pos()
}
// f.register_var(v)
f.args << v
@ -781,7 +771,7 @@ fn (p mut Parser) fn_args(f mut Fn) {
is_mut: is_mut
ptr: is_mut
line_nr: p.scanner.line_nr
scanner_pos: p.scanner.get_scanner_pos()
scanner_pos: p.scanner.get_scanner_pos()
}
f.register_var(v)
f.args << v

View File

@ -71,26 +71,28 @@ fn (p mut Parser) gen_fn_decl(f Fn, typ, str_args string) {
fn types_to_c(types []Type, table &Table) string {
mut sb := strings.new_builder(10)
for t in types {
if t.cat != .union_ && t.cat != .struct_ {
if t.cat != .union_ && t.cat != .struct_ && t.cat != .objc_interface {
continue
}
//if is_objc {
//sb.writeln('@interface $name : $objc_parent { @public')
//}
//if is_atomic {
//sb.write('_Atomic ')
//}
kind := if t.cat == .union_ {'union'} else {'struct'}
sb.writeln('$kind $t.name {')
if t.cat == .objc_interface {
sb.writeln('@interface $t.name : $t.parent { @public')
}
else {
kind := if t.cat == .union_ {'union'} else {'struct'}
sb.writeln('$kind $t.name {')
}
for field in t.fields {
sb.write('\t')
sb.writeln(table.cgen_name_type_pair(field.name,
field.typ) + ';')
}
sb.writeln('};\n')
//if is_objc {
//sb.writeln('@end')
//}
if t.cat == .objc_interface {
sb.writeln('@end')
}
}
return sb.str()
}

View File

@ -492,14 +492,17 @@ fn key_to_type_cat(tok Token) TypeCategory {
// also unions and interfaces
fn (p mut Parser) struct_decl() {
// V can generate Objective C for integration with Cocoa
// `[interface:ParentInterface]`
//is_objc := p.attr.starts_with('interface')
//objc_parent := if is_objc { p.attr.right(10) } else { '' }
// `[objc_interface:ParentInterface]`
is_objc := p.attr.starts_with('objc_interface')
objc_parent := if is_objc { p.attr.right(15) } else { '' }
// interface, union, struct
is_interface := p.tok == .key_interface
is_union := p.tok == .key_union
is_struct := p.tok == .key_struct
mut cat := key_to_type_cat(p.tok)
if is_objc {
cat = .objc_interface
}
p.fgen(p.tok.str() + ' ')
// Get type name
p.next()
@ -530,7 +533,11 @@ fn (p mut Parser) struct_decl() {
if p.pass == .decl && p.table.known_type_fast(typ) {
p.error('`$name` redeclared')
}
if !is_c {
if is_objc {
// Forward declaration of an Objective-C interface with `@class` :)
p.gen_typedef('@class $name;')
}
else if !is_c {
kind := if is_union {'union'} else {'struct'}
p.gen_typedef('typedef $kind $name $name;')
}
@ -544,6 +551,7 @@ fn (p mut Parser) struct_decl() {
typ.is_c = is_c
typ.is_placeholder = false
typ.cat = cat
typ.parent = objc_parent
p.table.rewrite_type(typ)
}
else {
@ -552,6 +560,7 @@ fn (p mut Parser) struct_decl() {
mod: p.mod
is_c: is_c
cat: cat
parent: objc_parent
}
}
// Struct `C.Foo` declaration, no body
@ -1715,12 +1724,6 @@ fn (p mut Parser) name_expr() string {
p.error('undefined: `$name`')
}
else {
if orig_name == 'i32' {
println('`i32` alias was removed, use `int` instead')
}
if orig_name == 'u8' {
println('`u8` alias was removed, use `byte` instead')
}
p.error('undefined: `$orig_name`')
}
} else {
@ -2852,42 +2855,11 @@ fn (p mut Parser) array_init() string {
}
fn (p mut Parser) struct_init(typ string) string {
//p.gen('/* struct init */')
p.is_struct_init = true
t := p.table.find_type(typ)
if p.gen_struct_init(typ, t) { return typ }
p.scanner.fmt_out.cut(typ.len)
ptr := typ.contains('*')
/*
if !ptr {
if p.is_c_struct_init {
// `face := C.FT_Face{}` => `FT_Face face;`
if p.tok == .rcbr {
p.is_empty_c_struct_init = true
p.check(.rcbr)
return typ
}
p.gen('(struct $typ) {')
p.is_c_struct_init = false
}
else {
p.gen('($typ /*str init */) {')
}
}
else {
// TODO tmp hack for 0 pointers init
// &User{!} ==> 0
if p.tok == .not {
p.next()
p.gen('0')
p.check(.rcbr)
return typ
}
p.is_alloc = true
//println('setting is_alloc=true (ret $typ)')
p.gen('($t.name*)memdup(&($t.name) {')
}
*/
mut did_gen_something := false
// Loop thru all struct init keys and assign values
// u := User{age:20, name:'bob'}
@ -3763,13 +3735,11 @@ fn (p mut Parser) js_decode() string {
fn (p mut Parser) attribute() {
p.check(.lsbr)
if p.tok == .key_interface {
p.check(.key_interface)
p.attr = p.check_name()
if p.tok == .colon {
p.check(.colon)
p.attr = 'interface:' + p.check_name()
} else {
p.attr = p.check_name()
}
p.attr = p.attr + ':' + p.check_name()
}
p.check(.rsbr)
if p.tok == .func || (p.tok == .key_pub && p.peek() == .func) {
p.fn_decl()

View File

@ -54,6 +54,7 @@ enum TypeCategory {
union_ // 5
c_struct
c_typedef
objc_interface // 8 Objective C @interface
array
}
@ -607,6 +608,11 @@ fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool {
// if expected == 'T' || expected.contains('<T>') {
// return true
// }
// TODO fn hack
if got.starts_with('fn ') && (expected.ends_with('fn') ||
expected.ends_with('Fn')) {
return true
}
// Allow pointer arithmetic
if expected=='void*' && got=='int' {
return true