vfmt: fix `sizeof(C.Type)`

pull/5917/head
Delyan Angelov 2020-07-21 16:28:30 +03:00
parent e1eb9c4ff1
commit 4cb9e651a6
2 changed files with 35 additions and 20 deletions

View File

@ -6,7 +6,6 @@ import strings
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/utsname.h> #include <sys/utsname.h>
pub const ( pub const (
path_separator = '/' path_separator = '/'
path_delimiter = ':' path_delimiter = ':'
@ -28,11 +27,14 @@ mut:
} }
fn C.uname(name voidptr) int fn C.uname(name voidptr) int
fn C.symlink(charptr, charptr) int
fn C.symlink(arg_1, arg_2 charptr) int
pub fn uname() Uname { pub fn uname() Uname {
mut u := Uname{} mut u := Uname{}
d := &C.utsname( malloc(int(sizeof(C.utsname))) ) utsize := sizeof(C.utsname)
x := malloc(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 { if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(byteptr(d.sysname)) u.sysname = cstring_to_vstring(byteptr(d.sysname))
u.nodename = cstring_to_vstring(byteptr(d.nodename)) u.nodename = cstring_to_vstring(byteptr(d.nodename))
@ -49,7 +51,6 @@ fn init_os_args(argc int, argv &&byte) []string {
// mut args := []string(make(0, argc, sizeof(string))) // mut args := []string(make(0, argc, sizeof(string)))
// mut args := []string{len:argc} // mut args := []string{len:argc}
for i in 0 .. argc { for i in 0 .. argc {
// args [i] = string(argv[i]) // args [i] = string(argv[i])
args << string(argv[i]) args << string(argv[i])
} }
@ -91,13 +92,10 @@ pub fn is_dir(path string) bool {
return res return res
} }
*/ */
/* /*
pub fn (mut f File) fseek(pos, mode int) { pub fn (mut f File) fseek(pos, mode int) {
} }
*/ */
// mkdir creates a new directory with the specified path. // mkdir creates a new directory with the specified path.
pub fn mkdir(path string) ?bool { pub fn mkdir(path string) ?bool {
if path == '.' { if path == '.' {
@ -109,7 +107,7 @@ pub fn mkdir(path string) ?bool {
k = 1 k = 1
} }
*/ */
apath := os.real_path(path) apath := real_path(path)
/* /*
$if linux { $if linux {
$if !android { $if !android {
@ -121,7 +119,10 @@ pub fn mkdir(path string) ?bool {
} }
} }
*/ */
r := unsafe {C.mkdir(charptr(apath.str), 511)} r := unsafe {
C.mkdir(charptr(apath.str), 511)
}
if r == -1 { if r == -1 {
return error(posix_get_error_msg(C.errno)) return error(posix_get_error_msg(C.errno))
} }

View File

@ -914,7 +914,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
if node.is_type { if node.is_type {
f.write('sizeof(') f.write('sizeof(')
if node.type_name != '' { if node.type_name != '' {
if f.is_external_name(node.type_name) {
f.write(node.type_name)
} else {
f.write(f.short_module(node.type_name)) f.write(f.short_module(node.type_name))
}
} else { } else {
f.write(f.type_to_str(node.typ)) f.write(f.type_to_str(node.typ))
} }
@ -1732,3 +1736,13 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
f.indent-- f.indent--
f.writeln(')\n') f.writeln(')\n')
} }
fn (mut f Fmt) is_external_name(name string) bool {
if name.len > 2 && name[0]==`C` && name[1]==`.` {
return true
}
if name.len > 3 && name[0]==`J` && name[1]==`S` && name[2] == `.` {
return true
}
return false
}