2019-12-05 18:47:29 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-09-02 14:17:20 +03:00
|
|
|
|
2019-12-05 18:47:29 +03:00
|
|
|
import (
|
|
|
|
strings
|
|
|
|
)
|
2019-09-02 14:17:20 +03:00
|
|
|
|
2019-12-19 23:52:45 +03:00
|
|
|
fn (p mut Parser) get_type2() Type {
|
2019-09-02 14:17:20 +03:00
|
|
|
mut mul := false
|
|
|
|
mut nr_muls := 0
|
|
|
|
mut typ := ''
|
2019-12-06 15:24:53 +03:00
|
|
|
cat := TypeCategory.struct_
|
2019-12-05 18:47:29 +03:00
|
|
|
// multiple returns
|
|
|
|
if p.tok == .lpar {
|
2019-12-19 23:52:45 +03:00
|
|
|
// p.warn('`()` are no longer necessary in multiple returns' +
|
|
|
|
// '\nuse `fn foo() int, int {` instead of `fn foo() (int, int) {`')
|
2019-12-05 18:47:29 +03:00
|
|
|
// if p.inside_tuple {p.error('unexpected (')}
|
|
|
|
// p.inside_tuple = true
|
|
|
|
p.check(.lpar)
|
|
|
|
mut types := []string
|
|
|
|
for {
|
|
|
|
types << p.get_type()
|
|
|
|
if p.tok != .comma {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.comma)
|
2019-12-18 10:17:07 +03:00
|
|
|
p.fspace()
|
2019-12-05 18:47:29 +03:00
|
|
|
}
|
|
|
|
p.check(.rpar)
|
|
|
|
// p.inside_tuple = false
|
|
|
|
typ = p.register_multi_return_stuct(types)
|
2019-12-19 23:52:45 +03:00
|
|
|
return Type{
|
2019-12-05 18:47:29 +03:00
|
|
|
name: typ
|
|
|
|
mod: p.mod
|
|
|
|
cat: cat
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
// fn type
|
2019-10-21 14:21:30 +03:00
|
|
|
if p.tok == .key_fn {
|
2019-12-19 23:52:45 +03:00
|
|
|
mut f := Fn{
|
|
|
|
name: '_'
|
|
|
|
mod: p.mod
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
p.next()
|
|
|
|
line_nr := p.scanner.line_nr
|
|
|
|
p.fn_args(mut f)
|
|
|
|
// Same line, it's a return type
|
|
|
|
if p.scanner.line_nr == line_nr {
|
2019-12-05 18:47:29 +03:00
|
|
|
if p.tok in [.name, .mul, .amp, .lsbr, .question, .lpar] {
|
2019-09-02 14:17:20 +03:00
|
|
|
f.typ = p.get_type()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.typ = 'void'
|
|
|
|
}
|
|
|
|
// println('fn return typ=$f.typ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.typ = 'void'
|
|
|
|
}
|
|
|
|
// Register anon fn type
|
2019-12-19 23:52:45 +03:00
|
|
|
fn_typ := Type{
|
|
|
|
name: f.typ_str() // 'fn (int, int) string'
|
|
|
|
|
2019-09-02 14:17:20 +03:00
|
|
|
mod: p.mod
|
|
|
|
func: f
|
2019-11-06 06:26:04 +03:00
|
|
|
cat: .func
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
2019-12-05 14:09:33 +03:00
|
|
|
p.table.register_type(fn_typ)
|
2019-09-02 14:17:20 +03:00
|
|
|
return fn_typ
|
|
|
|
}
|
|
|
|
// arrays ([]int)
|
2019-12-05 18:47:29 +03:00
|
|
|
mut arr_level := 0
|
2019-09-02 14:17:20 +03:00
|
|
|
is_question := p.tok == .question
|
|
|
|
if is_question {
|
|
|
|
p.check(.question)
|
|
|
|
}
|
2019-12-05 18:47:29 +03:00
|
|
|
for p.tok == .lsbr {
|
2019-09-02 14:17:20 +03:00
|
|
|
p.check(.lsbr)
|
|
|
|
// [10]int
|
2019-12-06 02:38:35 +03:00
|
|
|
if p.tok == .number || (p.tok == .name && !p.inside_const) {
|
|
|
|
if p.tok == .name {
|
|
|
|
typ += '[${p.mod}__$p.lit]'
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-06 02:38:35 +03:00
|
|
|
typ += '[$p.lit]'
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
2019-12-05 18:47:29 +03:00
|
|
|
arr_level++
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
|
|
|
p.check(.rsbr)
|
|
|
|
}
|
|
|
|
// map[string]int
|
|
|
|
if !p.builtin_mod && p.tok == .name && p.lit == 'map' {
|
|
|
|
p.next()
|
|
|
|
p.check(.lsbr)
|
|
|
|
key_type := p.check_name()
|
|
|
|
if key_type != 'string' {
|
|
|
|
p.error('maps only support string keys for now')
|
|
|
|
}
|
|
|
|
p.check(.rsbr)
|
2019-12-19 23:52:45 +03:00
|
|
|
val_type := p.get_type() // p.check_name()
|
2019-09-02 14:17:20 +03:00
|
|
|
typ = 'map_$val_type'
|
|
|
|
p.register_map(typ)
|
2019-12-19 23:52:45 +03:00
|
|
|
return Type{
|
|
|
|
name: typ
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
2019-12-05 18:47:29 +03:00
|
|
|
// ptr/ref
|
|
|
|
mut warn := false
|
2019-09-02 14:17:20 +03:00
|
|
|
for p.tok == .mul {
|
|
|
|
if p.first_pass() {
|
2019-12-05 18:47:29 +03:00
|
|
|
warn = true
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
|
|
|
mul = true
|
|
|
|
nr_muls++
|
|
|
|
p.check(.mul)
|
|
|
|
}
|
|
|
|
if p.tok == .amp {
|
|
|
|
mul = true
|
|
|
|
nr_muls++
|
|
|
|
p.check(.amp)
|
|
|
|
}
|
2019-12-05 18:47:29 +03:00
|
|
|
// generic type check
|
|
|
|
ti := p.cur_fn.dispatch_of.inst
|
|
|
|
if p.lit in ti.keys() {
|
|
|
|
typ += ti[p.lit]
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-05 18:47:29 +03:00
|
|
|
typ += p.lit
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
// C.Struct import
|
|
|
|
if p.lit == 'C' && p.peek() == .dot {
|
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
|
|
|
typ = p.lit
|
|
|
|
}
|
|
|
|
else {
|
2019-12-05 18:47:29 +03:00
|
|
|
if warn && p.mod != 'ui' {
|
|
|
|
p.warn('use `&Foo` instead of `*Foo`')
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
// Module specified? (e.g. gx.Image)
|
|
|
|
if p.peek() == .dot {
|
|
|
|
// try resolve full submodule
|
|
|
|
if !p.builtin_mod && p.import_table.known_alias(typ) {
|
|
|
|
mod := p.import_table.resolve_alias(typ)
|
|
|
|
if mod.contains('.') {
|
2019-12-05 18:47:29 +03:00
|
|
|
typ = mod_gen_name(mod)
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
|
|
|
typ += '__$p.lit'
|
|
|
|
}
|
|
|
|
mut t := p.table.find_type(typ)
|
|
|
|
// "typ" not found? try "mod__typ"
|
|
|
|
if t.name == '' && !p.builtin_mod {
|
|
|
|
// && !p.first_pass() {
|
2019-12-19 23:52:45 +03:00
|
|
|
if !typ.contains('array_') && p.mod != 'main' && !typ.contains('__') && !typ.starts_with('[') {
|
2019-09-02 14:17:20 +03:00
|
|
|
typ = p.prepend_mod(typ)
|
|
|
|
}
|
|
|
|
t = p.table.find_type(typ)
|
|
|
|
if t.name == '' && !p.pref.translated && !p.first_pass() && !typ.starts_with('[') {
|
|
|
|
println('get_type() bad type')
|
|
|
|
// println('all registered types:')
|
|
|
|
// for q in p.table.types {
|
|
|
|
// println(q.name)
|
|
|
|
// }
|
2019-12-19 23:52:45 +03:00
|
|
|
mut t_suggest,tc_suggest := p.table.find_misspelled_type(typ, p, 0.50)
|
2019-12-05 18:47:29 +03:00
|
|
|
if t_suggest.len > 0 {
|
|
|
|
t_suggest = '. did you mean: ($tc_suggest) `$t_suggest`'
|
|
|
|
}
|
|
|
|
p.error('unknown type `$typ`$t_suggest')
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 18:47:29 +03:00
|
|
|
else if !t.is_public && t.mod != p.mod && !p.is_vgen && t.name != '' && !p.first_pass() {
|
|
|
|
p.error('type `$t.name` is private')
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
|
|
|
if typ == 'void' {
|
|
|
|
p.error('unknown type `$typ`')
|
|
|
|
}
|
|
|
|
if mul {
|
|
|
|
typ += strings.repeat(`*`, nr_muls)
|
|
|
|
}
|
|
|
|
// Register an []array type
|
2019-12-05 18:47:29 +03:00
|
|
|
if arr_level > 0 {
|
|
|
|
// p.log('ARR TYPE="$typ" run=$p.pass')
|
2019-09-02 14:17:20 +03:00
|
|
|
// We come across "[]User" etc ?
|
2019-12-05 18:47:29 +03:00
|
|
|
for i := 0; i < arr_level; i++ {
|
|
|
|
typ = 'array_$typ'
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
p.register_array(typ)
|
|
|
|
}
|
|
|
|
p.next()
|
2019-09-23 23:17:06 +03:00
|
|
|
if is_question {
|
2019-09-02 14:17:20 +03:00
|
|
|
typ = 'Option_$typ'
|
|
|
|
p.table.register_type_with_parent(typ, 'Option')
|
|
|
|
}
|
2019-12-05 19:02:33 +03:00
|
|
|
// Because the code uses * to see if it's a pointer
|
|
|
|
if typ == 'byteptr' {
|
|
|
|
typ = 'byte*'
|
|
|
|
}
|
|
|
|
if typ == 'voidptr' {
|
2019-12-19 23:52:45 +03:00
|
|
|
// if !p.builtin_mod && p.mod != 'os' && p.mod != 'gx' && p.mod != 'gg' && !p.pref.translated {
|
|
|
|
// p.error('voidptr can only be used in unsafe code')
|
|
|
|
// }
|
2019-12-05 19:02:33 +03:00
|
|
|
typ = 'void*'
|
|
|
|
}
|
2019-11-30 13:09:05 +03:00
|
|
|
/*
|
2019-12-05 18:47:29 +03:00
|
|
|
TODO this is not needed?
|
2019-09-02 14:17:20 +03:00
|
|
|
if typ.last_index('__') > typ.index('__') {
|
|
|
|
p.error('2 __ in gettype(): typ="$typ"')
|
|
|
|
}
|
2019-11-30 13:09:05 +03:00
|
|
|
*/
|
2019-12-19 23:52:45 +03:00
|
|
|
|
|
|
|
return Type{
|
|
|
|
name: typ
|
|
|
|
cat: cat
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
}
|
2019-12-05 18:47:29 +03:00
|
|
|
|