A record field takes #c "TYPE" to set its C header type (or an array's element type), an enum takes a trailing #c "PREFIX" for its C constants, and a using field of a union or struct spelled #c "union"/"struct" lays its fields out as an anonymous C member. Saved IR keeps the spellings (version 63); Go and the .zx VM ignore them.
281 lines
11 KiB
Ziran
281 lines
11 KiB
Ziran
// Type spelling and record-field rules. Returned text borrows the input.
|
|
// Field scans accept bounded, zero-terminated IR buffers without measuring
|
|
// the whole record again for each field.
|
|
Text :: #import "compiler_text";
|
|
|
|
RecordField :: struct {
|
|
status: s32
|
|
next: s64
|
|
name: string
|
|
type: string
|
|
tag: string
|
|
c_type: string
|
|
is_using: bool
|
|
}
|
|
ElementType :: struct { valid: bool; element: string; capacity: s64 }
|
|
ForeignCall :: struct { kind: s32; module: string; receiver: string; symbol: string }
|
|
|
|
TargetKnown :: (target: string) -> bool {
|
|
return target == "c" || target == "go" || target == "zx"
|
|
}
|
|
|
|
// Empty means an ordinary language type. Foreign identities belong to the
|
|
// target that defines their representation, even when unused by an entry.
|
|
ForeignTypeTarget :: (foreign: string, is_map: bool) -> string {
|
|
if is_map || (foreign.count >= 3 && foreign[0:3] == "go:") { return "go" }
|
|
if foreign.count > 0 { return "unknown" }
|
|
return ""
|
|
}
|
|
|
|
ForeignImportSupported :: (target: string, foreign: string) -> bool {
|
|
if !TargetKnown(target) { return false }
|
|
if foreign == "go" { return target == foreign }
|
|
if foreign == "host" { return true }
|
|
if foreign == "c" { return target != "zx" }
|
|
return false
|
|
}
|
|
|
|
ProcedureTypeSupported :: (target: string, c_call: bool) -> bool {
|
|
return TargetKnown(target) && (!c_call || (target != "go" && target != "zx"))
|
|
}
|
|
|
|
ScalarByteWidth :: (type: string) -> s32 {
|
|
if type == "s8" || type == "u8" || type == "bool" { return 1 }
|
|
if type == "s16" || type == "u16" { return 2 }
|
|
if type == "s32" || type == "u32" || type == "float32" { return 4 }
|
|
if type == "s64" || type == "u64" || type == "float64" || type == "integer" { return 8 }
|
|
return 0
|
|
}
|
|
|
|
// The spelling of a whole `#c "SPELLING"` directive: the C type of a field,
|
|
// or the prefix of an enum's C constants. Identifier bytes, spaces and `*`
|
|
// only; empty for anything else.
|
|
CSpelling :: (directive: string) -> string {
|
|
if directive.count < 6 || directive[0:2] != "#c" || !Text.SpaceByte(directive[2]) { return "" }
|
|
literal := Trim(directive[3:])
|
|
if literal.count < 3 || literal[0] != #char "\"" || literal[literal.count - 1] != #char "\"" { return "" }
|
|
spelling := literal[1:literal.count - 1]
|
|
for byte: spelling {
|
|
if !Text.IdentifierByte(byte) && byte != #char " " && byte != #char "*" { return "" }
|
|
}
|
|
return spelling
|
|
}
|
|
|
|
NextField :: (body: string, at: s64, union_record: bool, name_limit: s64,
|
|
type_limit: s64, tag_workspace: []u8) -> RecordField {
|
|
result: RecordField
|
|
result.status = -1
|
|
result.next = at
|
|
if at < 0 || at > body.count { return result }
|
|
while Byte(body, at) != 0 {
|
|
end := at
|
|
quote := false
|
|
while Byte(body, end) != 0 {
|
|
byte := body[end]
|
|
if quote {
|
|
if byte == #char "\\" && Byte(body, end + 1) != 0 { end += 1 }
|
|
else if byte == #char "\"" { quote = false }
|
|
} else if byte == #char "\"" { quote = true }
|
|
else if byte == #char ";" || byte == #char "\n" { break }
|
|
end += 1
|
|
}
|
|
if quote { return result }
|
|
result.next = end
|
|
if Byte(body, end) != 0 { result.next += 1 }
|
|
line := Trim(body[at:end])
|
|
at = result.next
|
|
if line.count == 0 { continue }
|
|
if line.count >= 6 && line[0:5] == "using" && Text.SpaceByte(line[5]) {
|
|
result.is_using = true
|
|
line = Trim(line[5:])
|
|
}
|
|
colon := Find(line, #char ":", 0)
|
|
if colon < 0 { return result }
|
|
name := Trim(line[0:colon])
|
|
type := Trim(line[colon + 1:])
|
|
tag := Find(type, #char "#", 0)
|
|
if tag >= 0 {
|
|
directive := type[tag:]
|
|
result.c_type = CSpelling(directive)
|
|
if result.c_type.count >= type_limit { return result }
|
|
if result.c_type.count == 0 {
|
|
if union_record || directive.count < 8 || directive[0:7] != "#go_tag" ||
|
|
!Text.SpaceByte(directive[7]) { return result }
|
|
literal := Trim(directive[7:])
|
|
if literal.count == 0 || literal.count >= tag_workspace.count { return result }
|
|
decoded := Text.DecodeString(literal, tag_workspace)
|
|
if !decoded.valid { return result }
|
|
for byte: tag_workspace[0:decoded.count] { if byte == 0 { return result } }
|
|
result.tag = literal
|
|
}
|
|
type = Trim(type[0:tag])
|
|
}
|
|
if !Identifier(name) || name.count >= name_limit ||
|
|
type.count == 0 || type.count >= type_limit { return result }
|
|
result.status = 1
|
|
result.name = name
|
|
result.type = type
|
|
return result
|
|
}
|
|
result.status = 0
|
|
return result
|
|
}
|
|
|
|
SliceElement :: (type: string) -> ElementType {
|
|
if type.count < 2 || type[0:2] != "[]" { return .{} }
|
|
element := Trim(type[2:])
|
|
return .{element.count > 0, element, 0}
|
|
}
|
|
|
|
ArrayElement :: (type: string) -> ElementType {
|
|
if type.count < 4 || type[0] != #char "[" { return .{} }
|
|
close := Find(type, #char "]", 1)
|
|
if close <= 1 { return .{} }
|
|
element := Trim(type[close + 1:])
|
|
if element.count == 0 { return .{} }
|
|
digits := true
|
|
for byte: type[1:close] {
|
|
if byte < #char "0" || byte > #char "9" { digits = false; break }
|
|
}
|
|
capacity: s64 = -1 // The checker resolves constant expressions.
|
|
if digits {
|
|
capacity = 0
|
|
for byte: type[1:close] {
|
|
capacity = capacity * 10 + cast(s64)(byte - #char "0")
|
|
if capacity > 1048576 { return .{} }
|
|
}
|
|
}
|
|
return .{true, element, capacity}
|
|
}
|
|
|
|
// Fixed-width SIMD vectors spell their lane type and lane count, such as
|
|
// u8x16 or f32x8, and hold 128 or 256 bits. Native vectors, such as s16xN,
|
|
// hold as many 128-bit blocks as the code's instruction set has; their
|
|
// lanes are one block's. Float lanes are float32 and float64. An invalid
|
|
// spelling has zero lanes.
|
|
VectorShape :: struct { lanes: s32; lane: string }
|
|
|
|
VectorType :: (name: string) -> VectorShape {
|
|
if name.count < 4 || name.count > 6 { return .{} }
|
|
x := Find(name, #char "x", 2)
|
|
if x < 0 || x > 3 || x + 1 >= name.count || name[x + 1] == #char "0" { return .{} }
|
|
native := x + 2 == name.count && name[x + 1] == #char "N"
|
|
prefix := name[0:x]
|
|
lane := ""
|
|
bits: s64 = 0
|
|
if prefix == "u8" || prefix == "s8" { lane = prefix; bits = 8 }
|
|
else if prefix == "u16" || prefix == "s16" { lane = prefix; bits = 16 }
|
|
else if prefix == "u32" || prefix == "s32" { lane = prefix; bits = 32 }
|
|
else if prefix == "u64" || prefix == "s64" { lane = prefix; bits = 64 }
|
|
else if prefix == "f32" { lane = "float32"; bits = 32 }
|
|
else if prefix == "f64" { lane = "float64"; bits = 64 }
|
|
else { return .{} }
|
|
if native { return .{cast(s32)(128 / bits), lane} }
|
|
count: s64 = 0
|
|
for byte: name[x + 1:] {
|
|
if byte < #char "0" || byte > #char "9" { return .{} }
|
|
count = count * 10 + cast(s64)(byte - #char "0")
|
|
}
|
|
if count * bits != 128 && count * bits != 256 { return .{} }
|
|
return .{cast(s32) count, lane}
|
|
}
|
|
|
|
BuiltinName :: (name: string) -> bool {
|
|
if name.count < 2 || name.count > 7 { return false }
|
|
if VectorType(name).lanes > 0 { return true }
|
|
return name == "bool" || name == "void" || name == "s8" || name == "u8" ||
|
|
name == "s16" || name == "u16" || name == "s32" || name == "u32" ||
|
|
name == "s64" || name == "u64" || name == "isize" || name == "usize" ||
|
|
name == "float32" || name == "float64" || name == "string" || name == "int" ||
|
|
name == "float" || name == "Type" || name == "any"
|
|
}
|
|
|
|
// Concrete values that need no declaration or recursive layout walk.
|
|
// void and compile-time-only names cannot be record field values.
|
|
BuiltinValueName :: (name: string) -> bool {
|
|
return name == "string" || name == "isize" || name == "usize" ||
|
|
ScalarByteWidth(name) > 0 || VectorType(name).lanes > 0
|
|
}
|
|
|
|
MapPrimitive :: (name: string) -> bool {
|
|
return name == "MapInit" || name == "MapSet" || name == "MapGet" ||
|
|
name == "MapContains" || name == "MapDelete" || name == "MapClear" ||
|
|
name == "MapCount" || name == "MapKeys" || name == "MapLookup"
|
|
}
|
|
|
|
// Go methods use package.(Type).Method or package.(*Type).Method. The
|
|
// explicit go: prefix also permits package paths without a slash.
|
|
GoCall :: (target: string, receiver_limit: s64) -> ForeignCall {
|
|
explicit_go := target.count >= 3 && target[0:3] == "go:"
|
|
start: s64 = 0
|
|
if explicit_go { start = 3 }
|
|
method: s64 = -1
|
|
end: s64 = -1
|
|
slash := false
|
|
for at: start..target.count - 1 {
|
|
if target[at] == #char "/" { slash = true }
|
|
if target[at] == #char "." {
|
|
end = at
|
|
if Byte(target, at + 1) == #char "(" { method = at; break }
|
|
}
|
|
}
|
|
if end <= start || (!explicit_go && !slash) { return .{} }
|
|
module := target[start:end]
|
|
for byte: module {
|
|
if !Text.IdentifierByte(byte) && byte != #char "/" &&
|
|
byte != #char "." && byte != #char "-" { return .{} }
|
|
}
|
|
receiver := ""
|
|
name := end + 1
|
|
if method >= 0 {
|
|
close := Find(target, #char ")", method + 2)
|
|
if close < 0 || Byte(target, close + 1) != #char "." { return .{} }
|
|
receiver = target[method + 2:close]
|
|
if receiver.count == 0 || receiver.count >= receiver_limit { return .{} }
|
|
receiver_name := receiver
|
|
if receiver[0] == #char "*" { receiver_name = receiver[1:] }
|
|
if !Identifier(receiver_name) { return .{} }
|
|
name = close + 2
|
|
}
|
|
symbol := target[name:]
|
|
if !Identifier(symbol) { return .{} }
|
|
kind: s32 = 1
|
|
if method >= 0 { kind = 2 }
|
|
return .{kind, module, receiver, symbol}
|
|
}
|
|
|
|
GoTypeTarget :: (target: string) -> bool {
|
|
if target.count < 3 || target[0:3] != "go:" { return false }
|
|
call := GoCall(target, 0)
|
|
if call.kind != 1 { return false }
|
|
if target.count >= 11 && target[0:11] == "go:builtin." {
|
|
return call.module == "builtin" && (call.symbol == "any" || call.symbol == "error")
|
|
}
|
|
return true
|
|
}
|
|
|
|
#scope_file
|
|
Byte :: (source: string, at: s64) -> u8 {
|
|
if at < 0 || at >= source.count { return 0 }
|
|
return source[at]
|
|
}
|
|
Find :: (source: string, wanted: u8, at: s64) -> s64 {
|
|
while at < source.count {
|
|
if source[at] == wanted { return at }
|
|
at += 1
|
|
}
|
|
return -1
|
|
}
|
|
Trim :: (source: string) -> string {
|
|
range := Text.TrimRange(source)
|
|
return source[range.begin:range.end]
|
|
}
|
|
Identifier :: (source: string) -> bool {
|
|
if source.count == 0 { return false }
|
|
first := source[0]
|
|
if first != #char "_" && !(first >= #char "a" && first <= #char "z") &&
|
|
!(first >= #char "A" && first <= #char "Z") { return false }
|
|
for byte: source { if !Text.IdentifierByte(byte) { return false } }
|
|
return true
|
|
}
|