github.com/goplus/llgo@v0.8.3/c/c.go (about) 1 /* 2 * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package c 18 19 // typedef unsigned int uint; 20 import "C" 21 import "unsafe" 22 23 const ( 24 LLGoPackage = "decl" 25 ) 26 27 type ( 28 Char = int8 29 Int = C.int 30 Uint = C.uint 31 Long = int32 32 Ulong = uint32 33 LongLong = int64 34 UlongLong = uint64 35 Float = float32 36 Double = float64 37 Pointer = unsafe.Pointer 38 FilePtr = unsafe.Pointer 39 ) 40 41 type integer interface { 42 ~int | ~uint | ~uintptr | ~int32 | ~uint32 | ~int64 | ~uint64 43 } 44 45 //go:linkname Str llgo.cstr 46 func Str(string) *Char 47 48 // llgo:link Advance llgo.advance 49 func Advance[PtrT any](ptr PtrT, offset int) PtrT { return ptr } 50 51 // llgo:link Index llgo.index 52 func Index[T any, I integer](ptr *T, offset I) T { return *ptr } 53 54 //go:linkname Alloca llgo.alloca 55 func Alloca(size uintptr) Pointer 56 57 //go:linkname AllocaCStr llgo.allocaCStr 58 func AllocaCStr(s string) *Char 59 60 //go:linkname Unreachable llgo.unreachable 61 func Unreachable() 62 63 //go:linkname Malloc C.malloc 64 func Malloc(size uintptr) Pointer 65 66 //go:linkname Memcpy C.memcpy 67 func Memcpy(dst, src Pointer, n uintptr) Pointer 68 69 //go:linkname Memset C.memset 70 func Memset(s Pointer, c Int, n uintptr) Pointer 71 72 // ----------------------------------------------------------------------------- 73 74 //go:linkname GoStringData llgo.stringData 75 func GoStringData(string) *Char 76 77 // ----------------------------------------------------------------------------- 78 79 //go:linkname Remove C.remove 80 func Remove(path *Char) Int 81 82 // ----------------------------------------------------------------------------- 83 84 //go:linkname Exit C.exit 85 func Exit(Int) 86 87 // ----------------------------------------------------------------------------- 88 89 //go:linkname Rand C.rand 90 func Rand() Int 91 92 //go:linkname Qsort C.qsort 93 func Qsort(base Pointer, count, elem uintptr, compar func(a, b Pointer) Int) 94 95 // ----------------------------------------------------------------------------- 96 97 //go:linkname Stdin __stdinp 98 var Stdin FilePtr 99 100 //go:linkname Stdout __stdoutp 101 var Stdout FilePtr 102 103 //go:linkname Stderr __stderrp 104 var Stderr FilePtr 105 106 //go:linkname Printf C.printf 107 func Printf(format *Char, __llgo_va_list ...any) Int 108 109 //go:linkname Fprintf C.fprintf 110 func Fprintf(fp FilePtr, format *Char, __llgo_va_list ...any) Int 111 112 // ----------------------------------------------------------------------------- 113 114 //go:linkname Time C.time 115 func Time(*int32) int32 116 117 // ----------------------------------------------------------------------------- 118 119 type Option struct { 120 Name *Char 121 HasArg Int 122 Flag *Int 123 Val Int 124 } 125 126 //go:linkname Argc __llgo_argc 127 var Argc Int 128 129 //go:linkname Argv __llgo_argv 130 var Argv **Char 131 132 //go:linkname Optarg optarg 133 var Optarg *Char 134 135 //go:linkname Optind optind 136 var Optind Int 137 138 //go:linkname Opterr opterr 139 var Opterr Int 140 141 //go:linkname Optopt optopt 142 var Optopt Int 143 144 //go:linkname Getopt C.getopt 145 func Getopt(argc Int, argv **Char, optstring *Char) Int 146 147 //go:linkname GetoptLong C.getopt_long 148 func GetoptLong(argc Int, argv **Char, optstring *Char, longopts *Option, longindex *Int) Int 149 150 //go:linkname GetoptLongOnly C.getopt_long_only 151 func GetoptLongOnly(argc Int, argv **Char, optstring *Char, longopts *Option, longindex *Int) Int 152 153 // -----------------------------------------------------------------------------