github.com/gotranspile/cxgo@v0.3.7/types/ident.go (about) 1 package types 2 3 type AccessKind int 4 5 const ( 6 AccessUnknown = AccessKind(iota) 7 AccessDefine 8 AccessRead 9 AccessWrite 10 ) 11 12 type Usage struct { 13 *Ident 14 Access AccessKind 15 } 16 17 func useNodes(arr []Node, acc AccessKind) []Usage { 18 var out []Usage 19 for _, n := range arr { 20 if n == nil { 21 continue 22 } 23 for _, u := range n.Uses() { 24 if u.Access == AccessUnknown { 25 u.Access = acc 26 } 27 out = append(out, u) 28 } 29 } 30 return out 31 } 32 33 func UseRead(n ...Node) []Usage { 34 return useNodes(n, AccessRead) 35 } 36 37 func UseWrite(n ...Node) []Usage { 38 return useNodes(n, AccessWrite) 39 } 40 41 func NewUnnamed(typ Type) *Ident { 42 return &Ident{typ: typ} 43 } 44 45 func NewIdent(name string, typ Type) *Ident { 46 if typ == nil { 47 panic("must have a type; use UnkT if it's unknown") 48 } 49 return &Ident{Name: name, typ: typ} 50 } 51 52 func NewIdentGo(cname, goname string, typ Type) *Ident { 53 if typ == nil { 54 panic("must have a type; use UnkT if it's unknown") 55 } 56 return &Ident{Name: cname, GoName: goname, typ: typ} 57 } 58 59 type Ident struct { 60 typ Type 61 Name string 62 GoName string 63 } 64 65 func (e *Ident) IsUnnamed() bool { 66 return e.Name == "" && e.GoName == "" 67 } 68 69 func (e *Ident) String() string { 70 if e.GoName != "" { 71 return e.GoName 72 } 73 return e.Name 74 } 75 76 func (e *Ident) CType(exp Type) Type { 77 if exp == nil { 78 return e.typ 79 } 80 if tk := e.typ.Kind(); tk.IsUntyped() { 81 if tk.IsInt() { 82 if exp.Kind().IsInt() { 83 return exp 84 } 85 } 86 } 87 return e.typ 88 }