...

Source file src/pkg/cmd/cgo/gcc.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Annotate Ref in Prog with C types by parsing gcc debug output.
     6	// Conversion of debug output to Go types.
     7	
     8	package main
     9	
    10	import (
    11		"bytes"
    12		"debug/dwarf"
    13		"debug/elf"
    14		"debug/macho"
    15		"debug/pe"
    16		"encoding/binary"
    17		"errors"
    18		"flag"
    19		"fmt"
    20		"go/ast"
    21		"go/parser"
    22		"go/token"
    23		"internal/xcoff"
    24		"math"
    25		"os"
    26		"strconv"
    27		"strings"
    28		"unicode"
    29		"unicode/utf8"
    30	)
    31	
    32	var debugDefine = flag.Bool("debug-define", false, "print relevant #defines")
    33	var debugGcc = flag.Bool("debug-gcc", false, "print gcc invocations")
    34	
    35	var nameToC = map[string]string{
    36		"schar":         "signed char",
    37		"uchar":         "unsigned char",
    38		"ushort":        "unsigned short",
    39		"uint":          "unsigned int",
    40		"ulong":         "unsigned long",
    41		"longlong":      "long long",
    42		"ulonglong":     "unsigned long long",
    43		"complexfloat":  "float _Complex",
    44		"complexdouble": "double _Complex",
    45	}
    46	
    47	// cname returns the C name to use for C.s.
    48	// The expansions are listed in nameToC and also
    49	// struct_foo becomes "struct foo", and similarly for
    50	// union and enum.
    51	func cname(s string) string {
    52		if t, ok := nameToC[s]; ok {
    53			return t
    54		}
    55	
    56		if strings.HasPrefix(s, "struct_") {
    57			return "struct " + s[len("struct_"):]
    58		}
    59		if strings.HasPrefix(s, "union_") {
    60			return "union " + s[len("union_"):]
    61		}
    62		if strings.HasPrefix(s, "enum_") {
    63			return "enum " + s[len("enum_"):]
    64		}
    65		if strings.HasPrefix(s, "sizeof_") {
    66			return "sizeof(" + cname(s[len("sizeof_"):]) + ")"
    67		}
    68		return s
    69	}
    70	
    71	// DiscardCgoDirectives processes the import C preamble, and discards
    72	// all #cgo CFLAGS and LDFLAGS directives, so they don't make their
    73	// way into _cgo_export.h.
    74	func (f *File) DiscardCgoDirectives() {
    75		linesIn := strings.Split(f.Preamble, "\n")
    76		linesOut := make([]string, 0, len(linesIn))
    77		for _, line := range linesIn {
    78			l := strings.TrimSpace(line)
    79			if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(rune(l[4])) {
    80				linesOut = append(linesOut, line)
    81			} else {
    82				linesOut = append(linesOut, "")
    83			}
    84		}
    85		f.Preamble = strings.Join(linesOut, "\n")
    86	}
    87	
    88	// addToFlag appends args to flag. All flags are later written out onto the
    89	// _cgo_flags file for the build system to use.
    90	func (p *Package) addToFlag(flag string, args []string) {
    91		p.CgoFlags[flag] = append(p.CgoFlags[flag], args...)
    92		if flag == "CFLAGS" {
    93			// We'll also need these when preprocessing for dwarf information.
    94			// However, discard any -g options: we need to be able
    95			// to parse the debug info, so stick to what we expect.
    96			for _, arg := range args {
    97				if !strings.HasPrefix(arg, "-g") {
    98					p.GccOptions = append(p.GccOptions, arg)
    99				}
   100			}
   101		}
   102	}
   103	
   104	// splitQuoted splits the string s around each instance of one or more consecutive
   105	// white space characters while taking into account quotes and escaping, and
   106	// returns an array of substrings of s or an empty list if s contains only white space.
   107	// Single quotes and double quotes are recognized to prevent splitting within the
   108	// quoted region, and are removed from the resulting substrings. If a quote in s
   109	// isn't closed err will be set and r will have the unclosed argument as the
   110	// last element. The backslash is used for escaping.
   111	//
   112	// For example, the following string:
   113	//
   114	//     `a b:"c d" 'e''f'  "g\""`
   115	//
   116	// Would be parsed as:
   117	//
   118	//     []string{"a", "b:c d", "ef", `g"`}
   119	//
   120	func splitQuoted(s string) (r []string, err error) {
   121		var args []string
   122		arg := make([]rune, len(s))
   123		escaped := false
   124		quoted := false
   125		quote := '\x00'
   126		i := 0
   127		for _, r := range s {
   128			switch {
   129			case escaped:
   130				escaped = false
   131			case r == '\\':
   132				escaped = true
   133				continue
   134			case quote != 0:
   135				if r == quote {
   136					quote = 0
   137					continue
   138				}
   139			case r == '"' || r == '\'':
   140				quoted = true
   141				quote = r
   142				continue
   143			case unicode.IsSpace(r):
   144				if quoted || i > 0 {
   145					quoted = false
   146					args = append(args, string(arg[:i]))
   147					i = 0
   148				}
   149				continue
   150			}
   151			arg[i] = r
   152			i++
   153		}
   154		if quoted || i > 0 {
   155			args = append(args, string(arg[:i]))
   156		}
   157		if quote != 0 {
   158			err = errors.New("unclosed quote")
   159		} else if escaped {
   160			err = errors.New("unfinished escaping")
   161		}
   162		return args, err
   163	}
   164	
   165	// Translate rewrites f.AST, the original Go input, to remove
   166	// references to the imported package C, replacing them with
   167	// references to the equivalent Go types, functions, and variables.
   168	func (p *Package) Translate(f *File) {
   169		for _, cref := range f.Ref {
   170			// Convert C.ulong to C.unsigned long, etc.
   171			cref.Name.C = cname(cref.Name.Go)
   172		}
   173	
   174		var conv typeConv
   175		conv.Init(p.PtrSize, p.IntSize)
   176	
   177		p.loadDefines(f)
   178		p.typedefs = map[string]bool{}
   179		p.typedefList = nil
   180		numTypedefs := -1
   181		for len(p.typedefs) > numTypedefs {
   182			numTypedefs = len(p.typedefs)
   183			// Also ask about any typedefs we've seen so far.
   184			for _, info := range p.typedefList {
   185				n := &Name{
   186					Go: info.typedef,
   187					C:  info.typedef,
   188				}
   189				f.Name[info.typedef] = n
   190				f.NamePos[n] = info.pos
   191			}
   192			needType := p.guessKinds(f)
   193			if len(needType) > 0 {
   194				p.loadDWARF(f, &conv, needType)
   195			}
   196	
   197			// In godefs mode we're OK with the typedefs, which
   198			// will presumably also be defined in the file, we
   199			// don't want to resolve them to their base types.
   200			if *godefs {
   201				break
   202			}
   203		}
   204		p.prepareNames(f)
   205		if p.rewriteCalls(f) {
   206			// Add `import _cgo_unsafe "unsafe"` after the package statement.
   207			f.Edit.Insert(f.offset(f.AST.Name.End()), "; import _cgo_unsafe \"unsafe\"")
   208		}
   209		p.rewriteRef(f)
   210	}
   211	
   212	// loadDefines coerces gcc into spitting out the #defines in use
   213	// in the file f and saves relevant renamings in f.Name[name].Define.
   214	func (p *Package) loadDefines(f *File) {
   215		var b bytes.Buffer
   216		b.WriteString(builtinProlog)
   217		b.WriteString(f.Preamble)
   218		stdout := p.gccDefines(b.Bytes())
   219	
   220		for _, line := range strings.Split(stdout, "\n") {
   221			if len(line) < 9 || line[0:7] != "#define" {
   222				continue
   223			}
   224	
   225			line = strings.TrimSpace(line[8:])
   226	
   227			var key, val string
   228			spaceIndex := strings.Index(line, " ")
   229			tabIndex := strings.Index(line, "\t")
   230	
   231			if spaceIndex == -1 && tabIndex == -1 {
   232				continue
   233			} else if tabIndex == -1 || (spaceIndex != -1 && spaceIndex < tabIndex) {
   234				key = line[0:spaceIndex]
   235				val = strings.TrimSpace(line[spaceIndex:])
   236			} else {
   237				key = line[0:tabIndex]
   238				val = strings.TrimSpace(line[tabIndex:])
   239			}
   240	
   241			if key == "__clang__" {
   242				p.GccIsClang = true
   243			}
   244	
   245			if n := f.Name[key]; n != nil {
   246				if *debugDefine {
   247					fmt.Fprintf(os.Stderr, "#define %s %s\n", key, val)
   248				}
   249				n.Define = val
   250			}
   251		}
   252	}
   253	
   254	// guessKinds tricks gcc into revealing the kind of each
   255	// name xxx for the references C.xxx in the Go input.
   256	// The kind is either a constant, type, or variable.
   257	func (p *Package) guessKinds(f *File) []*Name {
   258		// Determine kinds for names we already know about,
   259		// like #defines or 'struct foo', before bothering with gcc.
   260		var names, needType []*Name
   261		optional := map[*Name]bool{}
   262		for _, key := range nameKeys(f.Name) {
   263			n := f.Name[key]
   264			// If we've already found this name as a #define
   265			// and we can translate it as a constant value, do so.
   266			if n.Define != "" {
   267				if i, err := strconv.ParseInt(n.Define, 0, 64); err == nil {
   268					n.Kind = "iconst"
   269					// Turn decimal into hex, just for consistency
   270					// with enum-derived constants. Otherwise
   271					// in the cgo -godefs output half the constants
   272					// are in hex and half are in whatever the #define used.
   273					n.Const = fmt.Sprintf("%#x", i)
   274				} else if n.Define[0] == '\'' {
   275					if _, err := parser.ParseExpr(n.Define); err == nil {
   276						n.Kind = "iconst"
   277						n.Const = n.Define
   278					}
   279				} else if n.Define[0] == '"' {
   280					if _, err := parser.ParseExpr(n.Define); err == nil {
   281						n.Kind = "sconst"
   282						n.Const = n.Define
   283					}
   284				}
   285	
   286				if n.IsConst() {
   287					continue
   288				}
   289			}
   290	
   291			// If this is a struct, union, or enum type name, no need to guess the kind.
   292			if strings.HasPrefix(n.C, "struct ") || strings.HasPrefix(n.C, "union ") || strings.HasPrefix(n.C, "enum ") {
   293				n.Kind = "type"
   294				needType = append(needType, n)
   295				continue
   296			}
   297	
   298			if goos == "darwin" && strings.HasSuffix(n.C, "Ref") {
   299				// For FooRef, find out if FooGetTypeID exists.
   300				s := n.C[:len(n.C)-3] + "GetTypeID"
   301				n := &Name{Go: s, C: s}
   302				names = append(names, n)
   303				optional[n] = true
   304			}
   305	
   306			// Otherwise, we'll need to find out from gcc.
   307			names = append(names, n)
   308		}
   309	
   310		// Bypass gcc if there's nothing left to find out.
   311		if len(names) == 0 {
   312			return needType
   313		}
   314	
   315		// Coerce gcc into telling us whether each name is a type, a value, or undeclared.
   316		// For names, find out whether they are integer constants.
   317		// We used to look at specific warning or error messages here, but that tied the
   318		// behavior too closely to specific versions of the compilers.
   319		// Instead, arrange that we can infer what we need from only the presence or absence
   320		// of an error on a specific line.
   321		//
   322		// For each name, we generate these lines, where xxx is the index in toSniff plus one.
   323		//
   324		//	#line xxx "not-declared"
   325		//	void __cgo_f_xxx_1(void) { __typeof__(name) *__cgo_undefined__1; }
   326		//	#line xxx "not-type"
   327		//	void __cgo_f_xxx_2(void) { name *__cgo_undefined__2; }
   328		//	#line xxx "not-int-const"
   329		//	void __cgo_f_xxx_3(void) { enum { __cgo_undefined__3 = (name)*1 }; }
   330		//	#line xxx "not-num-const"
   331		//	void __cgo_f_xxx_4(void) { static const double __cgo_undefined__4 = (name); }
   332		//	#line xxx "not-str-lit"
   333		//	void __cgo_f_xxx_5(void) { static const char __cgo_undefined__5[] = (name); }
   334		//
   335		// If we see an error at not-declared:xxx, the corresponding name is not declared.
   336		// If we see an error at not-type:xxx, the corresponding name is a type.
   337		// If we see an error at not-int-const:xxx, the corresponding name is not an integer constant.
   338		// If we see an error at not-num-const:xxx, the corresponding name is not a number constant.
   339		// If we see an error at not-str-lit:xxx, the corresponding name is not a string literal.
   340		//
   341		// The specific input forms are chosen so that they are valid C syntax regardless of
   342		// whether name denotes a type or an expression.
   343	
   344		var b bytes.Buffer
   345		b.WriteString(builtinProlog)
   346		b.WriteString(f.Preamble)
   347	
   348		for i, n := range names {
   349			fmt.Fprintf(&b, "#line %d \"not-declared\"\n"+
   350				"void __cgo_f_%d_1(void) { __typeof__(%s) *__cgo_undefined__1; }\n"+
   351				"#line %d \"not-type\"\n"+
   352				"void __cgo_f_%d_2(void) { %s *__cgo_undefined__2; }\n"+
   353				"#line %d \"not-int-const\"\n"+
   354				"void __cgo_f_%d_3(void) { enum { __cgo_undefined__3 = (%s)*1 }; }\n"+
   355				"#line %d \"not-num-const\"\n"+
   356				"void __cgo_f_%d_4(void) { static const double __cgo_undefined__4 = (%s); }\n"+
   357				"#line %d \"not-str-lit\"\n"+
   358				"void __cgo_f_%d_5(void) { static const char __cgo_undefined__5[] = (%s); }\n",
   359				i+1, i+1, n.C,
   360				i+1, i+1, n.C,
   361				i+1, i+1, n.C,
   362				i+1, i+1, n.C,
   363				i+1, i+1, n.C,
   364			)
   365		}
   366		fmt.Fprintf(&b, "#line 1 \"completed\"\n"+
   367			"int __cgo__1 = __cgo__2;\n")
   368	
   369		stderr := p.gccErrors(b.Bytes())
   370		if stderr == "" {
   371			fatalf("%s produced no output\non input:\n%s", p.gccBaseCmd()[0], b.Bytes())
   372		}
   373	
   374		completed := false
   375		sniff := make([]int, len(names))
   376		const (
   377			notType = 1 << iota
   378			notIntConst
   379			notNumConst
   380			notStrLiteral
   381			notDeclared
   382		)
   383		sawUnmatchedErrors := false
   384		for _, line := range strings.Split(stderr, "\n") {
   385			// Ignore warnings and random comments, with one
   386			// exception: newer GCC versions will sometimes emit
   387			// an error on a macro #define with a note referring
   388			// to where the expansion occurs. We care about where
   389			// the expansion occurs, so in that case treat the note
   390			// as an error.
   391			isError := strings.Contains(line, ": error:")
   392			isErrorNote := strings.Contains(line, ": note:") && sawUnmatchedErrors
   393			if !isError && !isErrorNote {
   394				continue
   395			}
   396	
   397			c1 := strings.Index(line, ":")
   398			if c1 < 0 {
   399				continue
   400			}
   401			c2 := strings.Index(line[c1+1:], ":")
   402			if c2 < 0 {
   403				continue
   404			}
   405			c2 += c1 + 1
   406	
   407			filename := line[:c1]
   408			i, _ := strconv.Atoi(line[c1+1 : c2])
   409			i--
   410			if i < 0 || i >= len(names) {
   411				if isError {
   412					sawUnmatchedErrors = true
   413				}
   414				continue
   415			}
   416	
   417			switch filename {
   418			case "completed":
   419				// Strictly speaking, there is no guarantee that seeing the error at completed:1
   420				// (at the end of the file) means we've seen all the errors from earlier in the file,
   421				// but usually it does. Certainly if we don't see the completed:1 error, we did
   422				// not get all the errors we expected.
   423				completed = true
   424	
   425			case "not-declared":
   426				sniff[i] |= notDeclared
   427			case "not-type":
   428				sniff[i] |= notType
   429			case "not-int-const":
   430				sniff[i] |= notIntConst
   431			case "not-num-const":
   432				sniff[i] |= notNumConst
   433			case "not-str-lit":
   434				sniff[i] |= notStrLiteral
   435			default:
   436				if isError {
   437					sawUnmatchedErrors = true
   438				}
   439				continue
   440			}
   441	
   442			sawUnmatchedErrors = false
   443		}
   444	
   445		if !completed {
   446			fatalf("%s did not produce error at completed:1\non input:\n%s\nfull error output:\n%s", p.gccBaseCmd()[0], b.Bytes(), stderr)
   447		}
   448	
   449		for i, n := range names {
   450			switch sniff[i] {
   451			default:
   452				if sniff[i]&notDeclared != 0 && optional[n] {
   453					// Ignore optional undeclared identifiers.
   454					// Don't report an error, and skip adding n to the needType array.
   455					continue
   456				}
   457				error_(f.NamePos[n], "could not determine kind of name for C.%s", fixGo(n.Go))
   458			case notStrLiteral | notType:
   459				n.Kind = "iconst"
   460			case notIntConst | notStrLiteral | notType:
   461				n.Kind = "fconst"
   462			case notIntConst | notNumConst | notType:
   463				n.Kind = "sconst"
   464			case notIntConst | notNumConst | notStrLiteral:
   465				n.Kind = "type"
   466			case notIntConst | notNumConst | notStrLiteral | notType:
   467				n.Kind = "not-type"
   468			}
   469			needType = append(needType, n)
   470		}
   471		if nerrors > 0 {
   472			// Check if compiling the preamble by itself causes any errors,
   473			// because the messages we've printed out so far aren't helpful
   474			// to users debugging preamble mistakes. See issue 8442.
   475			preambleErrors := p.gccErrors([]byte(f.Preamble))
   476			if len(preambleErrors) > 0 {
   477				error_(token.NoPos, "\n%s errors for preamble:\n%s", p.gccBaseCmd()[0], preambleErrors)
   478			}
   479	
   480			fatalf("unresolved names")
   481		}
   482	
   483		return needType
   484	}
   485	
   486	// loadDWARF parses the DWARF debug information generated
   487	// by gcc to learn the details of the constants, variables, and types
   488	// being referred to as C.xxx.
   489	func (p *Package) loadDWARF(f *File, conv *typeConv, names []*Name) {
   490		// Extract the types from the DWARF section of an object
   491		// from a well-formed C program. Gcc only generates DWARF info
   492		// for symbols in the object file, so it is not enough to print the
   493		// preamble and hope the symbols we care about will be there.
   494		// Instead, emit
   495		//	__typeof__(names[i]) *__cgo__i;
   496		// for each entry in names and then dereference the type we
   497		// learn for __cgo__i.
   498		var b bytes.Buffer
   499		b.WriteString(builtinProlog)
   500		b.WriteString(f.Preamble)
   501		b.WriteString("#line 1 \"cgo-dwarf-inference\"\n")
   502		for i, n := range names {
   503			fmt.Fprintf(&b, "__typeof__(%s) *__cgo__%d;\n", n.C, i)
   504			if n.Kind == "iconst" {
   505				fmt.Fprintf(&b, "enum { __cgo_enum__%d = %s };\n", i, n.C)
   506			}
   507		}
   508	
   509		// We create a data block initialized with the values,
   510		// so we can read them out of the object file.
   511		fmt.Fprintf(&b, "long long __cgodebug_ints[] = {\n")
   512		for _, n := range names {
   513			if n.Kind == "iconst" {
   514				fmt.Fprintf(&b, "\t%s,\n", n.C)
   515			} else {
   516				fmt.Fprintf(&b, "\t0,\n")
   517			}
   518		}
   519		// for the last entry, we cannot use 0, otherwise
   520		// in case all __cgodebug_data is zero initialized,
   521		// LLVM-based gcc will place the it in the __DATA.__common
   522		// zero-filled section (our debug/macho doesn't support
   523		// this)
   524		fmt.Fprintf(&b, "\t1\n")
   525		fmt.Fprintf(&b, "};\n")
   526	
   527		// do the same work for floats.
   528		fmt.Fprintf(&b, "double __cgodebug_floats[] = {\n")
   529		for _, n := range names {
   530			if n.Kind == "fconst" {
   531				fmt.Fprintf(&b, "\t%s,\n", n.C)
   532			} else {
   533				fmt.Fprintf(&b, "\t0,\n")
   534			}
   535		}
   536		fmt.Fprintf(&b, "\t1\n")
   537		fmt.Fprintf(&b, "};\n")
   538	
   539		// do the same work for strings.
   540		for i, n := range names {
   541			if n.Kind == "sconst" {
   542				fmt.Fprintf(&b, "const char __cgodebug_str__%d[] = %s;\n", i, n.C)
   543				fmt.Fprintf(&b, "const unsigned long long __cgodebug_strlen__%d = sizeof(%s)-1;\n", i, n.C)
   544			}
   545		}
   546	
   547		d, ints, floats, strs := p.gccDebug(b.Bytes(), len(names))
   548	
   549		// Scan DWARF info for top-level TagVariable entries with AttrName __cgo__i.
   550		types := make([]dwarf.Type, len(names))
   551		r := d.Reader()
   552		for {
   553			e, err := r.Next()
   554			if err != nil {
   555				fatalf("reading DWARF entry: %s", err)
   556			}
   557			if e == nil {
   558				break
   559			}
   560			switch e.Tag {
   561			case dwarf.TagVariable:
   562				name, _ := e.Val(dwarf.AttrName).(string)
   563				typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset)
   564				if name == "" || typOff == 0 {
   565					if e.Val(dwarf.AttrSpecification) != nil {
   566						// Since we are reading all the DWARF,
   567						// assume we will see the variable elsewhere.
   568						break
   569					}
   570					fatalf("malformed DWARF TagVariable entry")
   571				}
   572				if !strings.HasPrefix(name, "__cgo__") {
   573					break
   574				}
   575				typ, err := d.Type(typOff)
   576				if err != nil {
   577					fatalf("loading DWARF type: %s", err)
   578				}
   579				t, ok := typ.(*dwarf.PtrType)
   580				if !ok || t == nil {
   581					fatalf("internal error: %s has non-pointer type", name)
   582				}
   583				i, err := strconv.Atoi(name[7:])
   584				if err != nil {
   585					fatalf("malformed __cgo__ name: %s", name)
   586				}
   587				types[i] = t.Type
   588				p.recordTypedefs(t.Type, f.NamePos[names[i]])
   589			}
   590			if e.Tag != dwarf.TagCompileUnit {
   591				r.SkipChildren()
   592			}
   593		}
   594	
   595		// Record types and typedef information.
   596		for i, n := range names {
   597			if strings.HasSuffix(n.Go, "GetTypeID") && types[i].String() == "func() CFTypeID" {
   598				conv.getTypeIDs[n.Go[:len(n.Go)-9]] = true
   599			}
   600		}
   601		for i, n := range names {
   602			if types[i] == nil {
   603				continue
   604			}
   605			pos := f.NamePos[n]
   606			f, fok := types[i].(*dwarf.FuncType)
   607			if n.Kind != "type" && fok {
   608				n.Kind = "func"
   609				n.FuncType = conv.FuncType(f, pos)
   610			} else {
   611				n.Type = conv.Type(types[i], pos)
   612				switch n.Kind {
   613				case "iconst":
   614					if i < len(ints) {
   615						if _, ok := types[i].(*dwarf.UintType); ok {
   616							n.Const = fmt.Sprintf("%#x", uint64(ints[i]))
   617						} else {
   618							n.Const = fmt.Sprintf("%#x", ints[i])
   619						}
   620					}
   621				case "fconst":
   622					if i >= len(floats) {
   623						break
   624					}
   625					switch base(types[i]).(type) {
   626					case *dwarf.IntType, *dwarf.UintType:
   627						// This has an integer type so it's
   628						// not really a floating point
   629						// constant. This can happen when the
   630						// C compiler complains about using
   631						// the value as an integer constant,
   632						// but not as a general constant.
   633						// Treat this as a variable of the
   634						// appropriate type, not a constant,
   635						// to get C-style type handling,
   636						// avoiding the problem that C permits
   637						// uint64(-1) but Go does not.
   638						// See issue 26066.
   639						n.Kind = "var"
   640					default:
   641						n.Const = fmt.Sprintf("%f", floats[i])
   642					}
   643				case "sconst":
   644					if i < len(strs) {
   645						n.Const = fmt.Sprintf("%q", strs[i])
   646					}
   647				}
   648			}
   649			conv.FinishType(pos)
   650		}
   651	}
   652	
   653	// recordTypedefs remembers in p.typedefs all the typedefs used in dtypes and its children.
   654	func (p *Package) recordTypedefs(dtype dwarf.Type, pos token.Pos) {
   655		p.recordTypedefs1(dtype, pos, map[dwarf.Type]bool{})
   656	}
   657	
   658	func (p *Package) recordTypedefs1(dtype dwarf.Type, pos token.Pos, visited map[dwarf.Type]bool) {
   659		if dtype == nil {
   660			return
   661		}
   662		if visited[dtype] {
   663			return
   664		}
   665		visited[dtype] = true
   666		switch dt := dtype.(type) {
   667		case *dwarf.TypedefType:
   668			if strings.HasPrefix(dt.Name, "__builtin") {
   669				// Don't look inside builtin types. There be dragons.
   670				return
   671			}
   672			if !p.typedefs[dt.Name] {
   673				p.typedefs[dt.Name] = true
   674				p.typedefList = append(p.typedefList, typedefInfo{dt.Name, pos})
   675				p.recordTypedefs1(dt.Type, pos, visited)
   676			}
   677		case *dwarf.PtrType:
   678			p.recordTypedefs1(dt.Type, pos, visited)
   679		case *dwarf.ArrayType:
   680			p.recordTypedefs1(dt.Type, pos, visited)
   681		case *dwarf.QualType:
   682			p.recordTypedefs1(dt.Type, pos, visited)
   683		case *dwarf.FuncType:
   684			p.recordTypedefs1(dt.ReturnType, pos, visited)
   685			for _, a := range dt.ParamType {
   686				p.recordTypedefs1(a, pos, visited)
   687			}
   688		case *dwarf.StructType:
   689			for _, f := range dt.Field {
   690				p.recordTypedefs1(f.Type, pos, visited)
   691			}
   692		}
   693	}
   694	
   695	// prepareNames finalizes the Kind field of not-type names and sets
   696	// the mangled name of all names.
   697	func (p *Package) prepareNames(f *File) {
   698		for _, n := range f.Name {
   699			if n.Kind == "not-type" {
   700				if n.Define == "" {
   701					n.Kind = "var"
   702				} else {
   703					n.Kind = "macro"
   704					n.FuncType = &FuncType{
   705						Result: n.Type,
   706						Go: &ast.FuncType{
   707							Results: &ast.FieldList{List: []*ast.Field{{Type: n.Type.Go}}},
   708						},
   709					}
   710				}
   711			}
   712			p.mangleName(n)
   713		}
   714	}
   715	
   716	// mangleName does name mangling to translate names
   717	// from the original Go source files to the names
   718	// used in the final Go files generated by cgo.
   719	func (p *Package) mangleName(n *Name) {
   720		// When using gccgo variables have to be
   721		// exported so that they become global symbols
   722		// that the C code can refer to.
   723		prefix := "_C"
   724		if *gccgo && n.IsVar() {
   725			prefix = "C"
   726		}
   727		n.Mangle = prefix + n.Kind + "_" + n.Go
   728	}
   729	
   730	func (f *File) isMangledName(s string) bool {
   731		prefix := "_C"
   732		if strings.HasPrefix(s, prefix) {
   733			t := s[len(prefix):]
   734			for _, k := range nameKinds {
   735				if strings.HasPrefix(t, k+"_") {
   736					return true
   737				}
   738			}
   739		}
   740		return false
   741	}
   742	
   743	// rewriteCalls rewrites all calls that pass pointers to check that
   744	// they follow the rules for passing pointers between Go and C.
   745	// This reports whether the package needs to import unsafe as _cgo_unsafe.
   746	func (p *Package) rewriteCalls(f *File) bool {
   747		needsUnsafe := false
   748		// Walk backward so that in C.f1(C.f2()) we rewrite C.f2 first.
   749		for _, call := range f.Calls {
   750			if call.Done {
   751				continue
   752			}
   753			start := f.offset(call.Call.Pos())
   754			end := f.offset(call.Call.End())
   755			str, nu := p.rewriteCall(f, call)
   756			if str != "" {
   757				f.Edit.Replace(start, end, str)
   758				if nu {
   759					needsUnsafe = true
   760				}
   761			}
   762		}
   763		return needsUnsafe
   764	}
   765	
   766	// rewriteCall rewrites one call to add pointer checks.
   767	// If any pointer checks are required, we rewrite the call into a
   768	// function literal that calls _cgoCheckPointer for each pointer
   769	// argument and then calls the original function.
   770	// This returns the rewritten call and whether the package needs to
   771	// import unsafe as _cgo_unsafe.
   772	// If it returns the empty string, the call did not need to be rewritten.
   773	func (p *Package) rewriteCall(f *File, call *Call) (string, bool) {
   774		// This is a call to C.xxx; set goname to "xxx".
   775		// It may have already been mangled by rewriteName.
   776		var goname string
   777		switch fun := call.Call.Fun.(type) {
   778		case *ast.SelectorExpr:
   779			goname = fun.Sel.Name
   780		case *ast.Ident:
   781			goname = strings.TrimPrefix(fun.Name, "_C2func_")
   782			goname = strings.TrimPrefix(goname, "_Cfunc_")
   783		}
   784		if goname == "" || goname == "malloc" {
   785			return "", false
   786		}
   787		name := f.Name[goname]
   788		if name == nil || name.Kind != "func" {
   789			// Probably a type conversion.
   790			return "", false
   791		}
   792	
   793		params := name.FuncType.Params
   794		args := call.Call.Args
   795	
   796		// Avoid a crash if the number of arguments doesn't match
   797		// the number of parameters.
   798		// This will be caught when the generated file is compiled.
   799		if len(args) != len(params) {
   800			return "", false
   801		}
   802	
   803		any := false
   804		for i, param := range params {
   805			if p.needsPointerCheck(f, param.Go, args[i]) {
   806				any = true
   807				break
   808			}
   809		}
   810		if !any {
   811			return "", false
   812		}
   813	
   814		// We need to rewrite this call.
   815		//
   816		// Rewrite C.f(p) to
   817		//    func() {
   818		//            _cgo0 := p
   819		//            _cgoCheckPointer(_cgo0)
   820		//            C.f(_cgo0)
   821		//    }()
   822		// Using a function literal like this lets us evaluate the
   823		// function arguments only once while doing pointer checks.
   824		// This is particularly useful when passing additional arguments
   825		// to _cgoCheckPointer, as done in checkIndex and checkAddr.
   826		//
   827		// When the function argument is a conversion to unsafe.Pointer,
   828		// we unwrap the conversion before checking the pointer,
   829		// and then wrap again when calling C.f. This lets us check
   830		// the real type of the pointer in some cases. See issue #25941.
   831		//
   832		// When the call to C.f is deferred, we use an additional function
   833		// literal to evaluate the arguments at the right time.
   834		//    defer func() func() {
   835		//            _cgo0 := p
   836		//            return func() {
   837		//                    _cgoCheckPointer(_cgo0)
   838		//                    C.f(_cgo0)
   839		//            }
   840		//    }()()
   841		// This works because the defer statement evaluates the first
   842		// function literal in order to get the function to call.
   843	
   844		var sb bytes.Buffer
   845		sb.WriteString("func() ")
   846		if call.Deferred {
   847			sb.WriteString("func() ")
   848		}
   849	
   850		needsUnsafe := false
   851		result := false
   852		twoResults := false
   853		if !call.Deferred {
   854			// Check whether this call expects two results.
   855			for _, ref := range f.Ref {
   856				if ref.Expr != &call.Call.Fun {
   857					continue
   858				}
   859				if ref.Context == ctxCall2 {
   860					sb.WriteString("(")
   861					result = true
   862					twoResults = true
   863				}
   864				break
   865			}
   866	
   867			// Add the result type, if any.
   868			if name.FuncType.Result != nil {
   869				rtype := p.rewriteUnsafe(name.FuncType.Result.Go)
   870				if rtype != name.FuncType.Result.Go {
   871					needsUnsafe = true
   872				}
   873				sb.WriteString(gofmtLine(rtype))
   874				result = true
   875			}
   876	
   877			// Add the second result type, if any.
   878			if twoResults {
   879				if name.FuncType.Result == nil {
   880					// An explicit void result looks odd but it
   881					// seems to be how cgo has worked historically.
   882					sb.WriteString("_Ctype_void")
   883				}
   884				sb.WriteString(", error)")
   885			}
   886		}
   887	
   888		sb.WriteString("{ ")
   889	
   890		// Define _cgoN for each argument value.
   891		// Write _cgoCheckPointer calls to sbCheck.
   892		var sbCheck bytes.Buffer
   893		for i, param := range params {
   894			origArg := args[i]
   895			arg, nu := p.mangle(f, &args[i])
   896			if nu {
   897				needsUnsafe = true
   898			}
   899	
   900			// Use "var x T = ..." syntax to explicitly convert untyped
   901			// constants to the parameter type, to avoid a type mismatch.
   902			ptype := p.rewriteUnsafe(param.Go)
   903	
   904			if !p.needsPointerCheck(f, param.Go, args[i]) || param.BadPointer {
   905				if ptype != param.Go {
   906					needsUnsafe = true
   907				}
   908				fmt.Fprintf(&sb, "var _cgo%d %s = %s; ", i,
   909					gofmtLine(ptype), gofmtPos(arg, origArg.Pos()))
   910				continue
   911			}
   912	
   913			// Check for &a[i].
   914			if p.checkIndex(&sb, &sbCheck, arg, i) {
   915				continue
   916			}
   917	
   918			// Check for &x.
   919			if p.checkAddr(&sb, &sbCheck, arg, i) {
   920				continue
   921			}
   922	
   923			fmt.Fprintf(&sb, "_cgo%d := %s; ", i, gofmtPos(arg, origArg.Pos()))
   924			fmt.Fprintf(&sbCheck, "_cgoCheckPointer(_cgo%d); ", i)
   925		}
   926	
   927		if call.Deferred {
   928			sb.WriteString("return func() { ")
   929		}
   930	
   931		// Write out the calls to _cgoCheckPointer.
   932		sb.WriteString(sbCheck.String())
   933	
   934		if result {
   935			sb.WriteString("return ")
   936		}
   937	
   938		m, nu := p.mangle(f, &call.Call.Fun)
   939		if nu {
   940			needsUnsafe = true
   941		}
   942		sb.WriteString(gofmtLine(m))
   943	
   944		sb.WriteString("(")
   945		for i := range params {
   946			if i > 0 {
   947				sb.WriteString(", ")
   948			}
   949			fmt.Fprintf(&sb, "_cgo%d", i)
   950		}
   951		sb.WriteString("); ")
   952		if call.Deferred {
   953			sb.WriteString("}")
   954		}
   955		sb.WriteString("}")
   956		if call.Deferred {
   957			sb.WriteString("()")
   958		}
   959		sb.WriteString("()")
   960	
   961		return sb.String(), needsUnsafe
   962	}
   963	
   964	// needsPointerCheck reports whether the type t needs a pointer check.
   965	// This is true if t is a pointer and if the value to which it points
   966	// might contain a pointer.
   967	func (p *Package) needsPointerCheck(f *File, t ast.Expr, arg ast.Expr) bool {
   968		// An untyped nil does not need a pointer check, and when
   969		// _cgoCheckPointer returns the untyped nil the type assertion we
   970		// are going to insert will fail.  Easier to just skip nil arguments.
   971		// TODO: Note that this fails if nil is shadowed.
   972		if id, ok := arg.(*ast.Ident); ok && id.Name == "nil" {
   973			return false
   974		}
   975	
   976		return p.hasPointer(f, t, true)
   977	}
   978	
   979	// hasPointer is used by needsPointerCheck. If top is true it returns
   980	// whether t is or contains a pointer that might point to a pointer.
   981	// If top is false it reports whether t is or contains a pointer.
   982	// f may be nil.
   983	func (p *Package) hasPointer(f *File, t ast.Expr, top bool) bool {
   984		switch t := t.(type) {
   985		case *ast.ArrayType:
   986			if t.Len == nil {
   987				if !top {
   988					return true
   989				}
   990				return p.hasPointer(f, t.Elt, false)
   991			}
   992			return p.hasPointer(f, t.Elt, top)
   993		case *ast.StructType:
   994			for _, field := range t.Fields.List {
   995				if p.hasPointer(f, field.Type, top) {
   996					return true
   997				}
   998			}
   999			return false
  1000		case *ast.StarExpr: // Pointer type.
  1001			if !top {
  1002				return true
  1003			}
  1004			// Check whether this is a pointer to a C union (or class)
  1005			// type that contains a pointer.
  1006			if unionWithPointer[t.X] {
  1007				return true
  1008			}
  1009			return p.hasPointer(f, t.X, false)
  1010		case *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.ChanType:
  1011			return true
  1012		case *ast.Ident:
  1013			// TODO: Handle types defined within function.
  1014			for _, d := range p.Decl {
  1015				gd, ok := d.(*ast.GenDecl)
  1016				if !ok || gd.Tok != token.TYPE {
  1017					continue
  1018				}
  1019				for _, spec := range gd.Specs {
  1020					ts, ok := spec.(*ast.TypeSpec)
  1021					if !ok {
  1022						continue
  1023					}
  1024					if ts.Name.Name == t.Name {
  1025						return p.hasPointer(f, ts.Type, top)
  1026					}
  1027				}
  1028			}
  1029			if def := typedef[t.Name]; def != nil {
  1030				return p.hasPointer(f, def.Go, top)
  1031			}
  1032			if t.Name == "string" {
  1033				return !top
  1034			}
  1035			if t.Name == "error" {
  1036				return true
  1037			}
  1038			if goTypes[t.Name] != nil {
  1039				return false
  1040			}
  1041			// We can't figure out the type. Conservative
  1042			// approach is to assume it has a pointer.
  1043			return true
  1044		case *ast.SelectorExpr:
  1045			if l, ok := t.X.(*ast.Ident); !ok || l.Name != "C" {
  1046				// Type defined in a different package.
  1047				// Conservative approach is to assume it has a
  1048				// pointer.
  1049				return true
  1050			}
  1051			if f == nil {
  1052				// Conservative approach: assume pointer.
  1053				return true
  1054			}
  1055			name := f.Name[t.Sel.Name]
  1056			if name != nil && name.Kind == "type" && name.Type != nil && name.Type.Go != nil {
  1057				return p.hasPointer(f, name.Type.Go, top)
  1058			}
  1059			// We can't figure out the type. Conservative
  1060			// approach is to assume it has a pointer.
  1061			return true
  1062		default:
  1063			error_(t.Pos(), "could not understand type %s", gofmt(t))
  1064			return true
  1065		}
  1066	}
  1067	
  1068	// mangle replaces references to C names in arg with the mangled names,
  1069	// rewriting calls when it finds them.
  1070	// It removes the corresponding references in f.Ref and f.Calls, so that we
  1071	// don't try to do the replacement again in rewriteRef or rewriteCall.
  1072	func (p *Package) mangle(f *File, arg *ast.Expr) (ast.Expr, bool) {
  1073		needsUnsafe := false
  1074		f.walk(arg, ctxExpr, func(f *File, arg interface{}, context astContext) {
  1075			px, ok := arg.(*ast.Expr)
  1076			if !ok {
  1077				return
  1078			}
  1079			sel, ok := (*px).(*ast.SelectorExpr)
  1080			if ok {
  1081				if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" {
  1082					return
  1083				}
  1084	
  1085				for _, r := range f.Ref {
  1086					if r.Expr == px {
  1087						*px = p.rewriteName(f, r)
  1088						r.Done = true
  1089						break
  1090					}
  1091				}
  1092	
  1093				return
  1094			}
  1095	
  1096			call, ok := (*px).(*ast.CallExpr)
  1097			if !ok {
  1098				return
  1099			}
  1100	
  1101			for _, c := range f.Calls {
  1102				if !c.Done && c.Call.Lparen == call.Lparen {
  1103					cstr, nu := p.rewriteCall(f, c)
  1104					if cstr != "" {
  1105						// Smuggle the rewritten call through an ident.
  1106						*px = ast.NewIdent(cstr)
  1107						if nu {
  1108							needsUnsafe = true
  1109						}
  1110						c.Done = true
  1111					}
  1112				}
  1113			}
  1114		})
  1115		return *arg, needsUnsafe
  1116	}
  1117	
  1118	// checkIndex checks whether arg has the form &a[i], possibly inside
  1119	// type conversions. If so, then in the general case it writes
  1120	//    _cgoIndexNN := a
  1121	//    _cgoNN := &cgoIndexNN[i] // with type conversions, if any
  1122	// to sb, and writes
  1123	//    _cgoCheckPointer(_cgoNN, _cgoIndexNN)
  1124	// to sbCheck, and returns true. If a is a simple variable or field reference,
  1125	// it writes
  1126	//    _cgoIndexNN := &a
  1127	// and dereferences the uses of _cgoIndexNN. Taking the address avoids
  1128	// making a copy of an array.
  1129	//
  1130	// This tells _cgoCheckPointer to check the complete contents of the
  1131	// slice or array being indexed, but no other part of the memory allocation.
  1132	func (p *Package) checkIndex(sb, sbCheck *bytes.Buffer, arg ast.Expr, i int) bool {
  1133		// Strip type conversions.
  1134		x := arg
  1135		for {
  1136			c, ok := x.(*ast.CallExpr)
  1137			if !ok || len(c.Args) != 1 || !p.isType(c.Fun) {
  1138				break
  1139			}
  1140			x = c.Args[0]
  1141		}
  1142		u, ok := x.(*ast.UnaryExpr)
  1143		if !ok || u.Op != token.AND {
  1144			return false
  1145		}
  1146		index, ok := u.X.(*ast.IndexExpr)
  1147		if !ok {
  1148			return false
  1149		}
  1150	
  1151		addr := ""
  1152		deref := ""
  1153		if p.isVariable(index.X) {
  1154			addr = "&"
  1155			deref = "*"
  1156		}
  1157	
  1158		fmt.Fprintf(sb, "_cgoIndex%d := %s%s; ", i, addr, gofmtPos(index.X, index.X.Pos()))
  1159		origX := index.X
  1160		index.X = ast.NewIdent(fmt.Sprintf("_cgoIndex%d", i))
  1161		if deref == "*" {
  1162			index.X = &ast.StarExpr{X: index.X}
  1163		}
  1164		fmt.Fprintf(sb, "_cgo%d := %s; ", i, gofmtPos(arg, arg.Pos()))
  1165		index.X = origX
  1166	
  1167		fmt.Fprintf(sbCheck, "_cgoCheckPointer(_cgo%d, %s_cgoIndex%d); ", i, deref, i)
  1168	
  1169		return true
  1170	}
  1171	
  1172	// checkAddr checks whether arg has the form &x, possibly inside type
  1173	// conversions. If so, it writes
  1174	//    _cgoBaseNN := &x
  1175	//    _cgoNN := _cgoBaseNN // with type conversions, if any
  1176	// to sb, and writes
  1177	//    _cgoCheckPointer(_cgoBaseNN, true)
  1178	// to sbCheck, and returns true. This tells _cgoCheckPointer to check
  1179	// just the contents of the pointer being passed, not any other part
  1180	// of the memory allocation. This is run after checkIndex, which looks
  1181	// for the special case of &a[i], which requires different checks.
  1182	func (p *Package) checkAddr(sb, sbCheck *bytes.Buffer, arg ast.Expr, i int) bool {
  1183		// Strip type conversions.
  1184		px := &arg
  1185		for {
  1186			c, ok := (*px).(*ast.CallExpr)
  1187			if !ok || len(c.Args) != 1 || !p.isType(c.Fun) {
  1188				break
  1189			}
  1190			px = &c.Args[0]
  1191		}
  1192		if u, ok := (*px).(*ast.UnaryExpr); !ok || u.Op != token.AND {
  1193			return false
  1194		}
  1195	
  1196		fmt.Fprintf(sb, "_cgoBase%d := %s; ", i, gofmtPos(*px, (*px).Pos()))
  1197	
  1198		origX := *px
  1199		*px = ast.NewIdent(fmt.Sprintf("_cgoBase%d", i))
  1200		fmt.Fprintf(sb, "_cgo%d := %s; ", i, gofmtPos(arg, arg.Pos()))
  1201		*px = origX
  1202	
  1203		// Use "0 == 0" to do the right thing in the unlikely event
  1204		// that "true" is shadowed.
  1205		fmt.Fprintf(sbCheck, "_cgoCheckPointer(_cgoBase%d, 0 == 0); ", i)
  1206	
  1207		return true
  1208	}
  1209	
  1210	// isType reports whether the expression is definitely a type.
  1211	// This is conservative--it returns false for an unknown identifier.
  1212	func (p *Package) isType(t ast.Expr) bool {
  1213		switch t := t.(type) {
  1214		case *ast.SelectorExpr:
  1215			id, ok := t.X.(*ast.Ident)
  1216			if !ok {
  1217				return false
  1218			}
  1219			if id.Name == "unsafe" && t.Sel.Name == "Pointer" {
  1220				return true
  1221			}
  1222			if id.Name == "C" && typedef["_Ctype_"+t.Sel.Name] != nil {
  1223				return true
  1224			}
  1225			return false
  1226		case *ast.Ident:
  1227			// TODO: This ignores shadowing.
  1228			switch t.Name {
  1229			case "unsafe.Pointer", "bool", "byte",
  1230				"complex64", "complex128",
  1231				"error",
  1232				"float32", "float64",
  1233				"int", "int8", "int16", "int32", "int64",
  1234				"rune", "string",
  1235				"uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
  1236	
  1237				return true
  1238			}
  1239			if strings.HasPrefix(t.Name, "_Ctype_") {
  1240				return true
  1241			}
  1242		case *ast.ParenExpr:
  1243			return p.isType(t.X)
  1244		case *ast.StarExpr:
  1245			return p.isType(t.X)
  1246		case *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType,
  1247			*ast.MapType, *ast.ChanType:
  1248	
  1249			return true
  1250		}
  1251		return false
  1252	}
  1253	
  1254	// isVariable reports whether x is a variable, possibly with field references.
  1255	func (p *Package) isVariable(x ast.Expr) bool {
  1256		switch x := x.(type) {
  1257		case *ast.Ident:
  1258			return true
  1259		case *ast.SelectorExpr:
  1260			return p.isVariable(x.X)
  1261		case *ast.IndexExpr:
  1262			return true
  1263		}
  1264		return false
  1265	}
  1266	
  1267	// rewriteUnsafe returns a version of t with references to unsafe.Pointer
  1268	// rewritten to use _cgo_unsafe.Pointer instead.
  1269	func (p *Package) rewriteUnsafe(t ast.Expr) ast.Expr {
  1270		switch t := t.(type) {
  1271		case *ast.Ident:
  1272			// We don't see a SelectorExpr for unsafe.Pointer;
  1273			// this is created by code in this file.
  1274			if t.Name == "unsafe.Pointer" {
  1275				return ast.NewIdent("_cgo_unsafe.Pointer")
  1276			}
  1277		case *ast.ArrayType:
  1278			t1 := p.rewriteUnsafe(t.Elt)
  1279			if t1 != t.Elt {
  1280				r := *t
  1281				r.Elt = t1
  1282				return &r
  1283			}
  1284		case *ast.StructType:
  1285			changed := false
  1286			fields := *t.Fields
  1287			fields.List = nil
  1288			for _, f := range t.Fields.List {
  1289				ft := p.rewriteUnsafe(f.Type)
  1290				if ft == f.Type {
  1291					fields.List = append(fields.List, f)
  1292				} else {
  1293					fn := *f
  1294					fn.Type = ft
  1295					fields.List = append(fields.List, &fn)
  1296					changed = true
  1297				}
  1298			}
  1299			if changed {
  1300				r := *t
  1301				r.Fields = &fields
  1302				return &r
  1303			}
  1304		case *ast.StarExpr: // Pointer type.
  1305			x1 := p.rewriteUnsafe(t.X)
  1306			if x1 != t.X {
  1307				r := *t
  1308				r.X = x1
  1309				return &r
  1310			}
  1311		}
  1312		return t
  1313	}
  1314	
  1315	// rewriteRef rewrites all the C.xxx references in f.AST to refer to the
  1316	// Go equivalents, now that we have figured out the meaning of all
  1317	// the xxx. In *godefs mode, rewriteRef replaces the names
  1318	// with full definitions instead of mangled names.
  1319	func (p *Package) rewriteRef(f *File) {
  1320		// Keep a list of all the functions, to remove the ones
  1321		// only used as expressions and avoid generating bridge
  1322		// code for them.
  1323		functions := make(map[string]bool)
  1324	
  1325		for _, n := range f.Name {
  1326			if n.Kind == "func" {
  1327				functions[n.Go] = false
  1328			}
  1329		}
  1330	
  1331		// Now that we have all the name types filled in,
  1332		// scan through the Refs to identify the ones that
  1333		// are trying to do a ,err call. Also check that
  1334		// functions are only used in calls.
  1335		for _, r := range f.Ref {
  1336			if r.Name.IsConst() && r.Name.Const == "" {
  1337				error_(r.Pos(), "unable to find value of constant C.%s", fixGo(r.Name.Go))
  1338			}
  1339	
  1340			if r.Name.Kind == "func" {
  1341				switch r.Context {
  1342				case ctxCall, ctxCall2:
  1343					functions[r.Name.Go] = true
  1344				}
  1345			}
  1346	
  1347			expr := p.rewriteName(f, r)
  1348	
  1349			if *godefs {
  1350				// Substitute definition for mangled type name.
  1351				if id, ok := expr.(*ast.Ident); ok {
  1352					if t := typedef[id.Name]; t != nil {
  1353						expr = t.Go
  1354					}
  1355					if id.Name == r.Name.Mangle && r.Name.Const != "" {
  1356						expr = ast.NewIdent(r.Name.Const)
  1357					}
  1358				}
  1359			}
  1360	
  1361			// Copy position information from old expr into new expr,
  1362			// in case expression being replaced is first on line.
  1363			// See golang.org/issue/6563.
  1364			pos := (*r.Expr).Pos()
  1365			if x, ok := expr.(*ast.Ident); ok {
  1366				expr = &ast.Ident{NamePos: pos, Name: x.Name}
  1367			}
  1368	
  1369			// Change AST, because some later processing depends on it,
  1370			// and also because -godefs mode still prints the AST.
  1371			old := *r.Expr
  1372			*r.Expr = expr
  1373	
  1374			// Record source-level edit for cgo output.
  1375			if !r.Done {
  1376				// Prepend a space in case the earlier code ends
  1377				// with '/', which would give us a "//" comment.
  1378				repl := " " + gofmtPos(expr, old.Pos())
  1379				end := fset.Position(old.End())
  1380				// Subtract 1 from the column if we are going to
  1381				// append a close parenthesis. That will set the
  1382				// correct column for the following characters.
  1383				sub := 0
  1384				if r.Name.Kind != "type" {
  1385					sub = 1
  1386				}
  1387				if end.Column > sub {
  1388					repl = fmt.Sprintf("%s /*line :%d:%d*/", repl, end.Line, end.Column-sub)
  1389				}
  1390				if r.Name.Kind != "type" {
  1391					repl = "(" + repl + ")"
  1392				}
  1393				f.Edit.Replace(f.offset(old.Pos()), f.offset(old.End()), repl)
  1394			}
  1395		}
  1396	
  1397		// Remove functions only used as expressions, so their respective
  1398		// bridge functions are not generated.
  1399		for name, used := range functions {
  1400			if !used {
  1401				delete(f.Name, name)
  1402			}
  1403		}
  1404	}
  1405	
  1406	// rewriteName returns the expression used to rewrite a reference.
  1407	func (p *Package) rewriteName(f *File, r *Ref) ast.Expr {
  1408		var expr ast.Expr = ast.NewIdent(r.Name.Mangle) // default
  1409		switch r.Context {
  1410		case ctxCall, ctxCall2:
  1411			if r.Name.Kind != "func" {
  1412				if r.Name.Kind == "type" {
  1413					r.Context = ctxType
  1414					if r.Name.Type == nil {
  1415						error_(r.Pos(), "invalid conversion to C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
  1416						break
  1417					}
  1418					expr = r.Name.Type.Go
  1419					break
  1420				}
  1421				error_(r.Pos(), "call of non-function C.%s", fixGo(r.Name.Go))
  1422				break
  1423			}
  1424			if r.Context == ctxCall2 {
  1425				if r.Name.Go == "_CMalloc" {
  1426					error_(r.Pos(), "no two-result form for C.malloc")
  1427					break
  1428				}
  1429				// Invent new Name for the two-result function.
  1430				n := f.Name["2"+r.Name.Go]
  1431				if n == nil {
  1432					n = new(Name)
  1433					*n = *r.Name
  1434					n.AddError = true
  1435					n.Mangle = "_C2func_" + n.Go
  1436					f.Name["2"+r.Name.Go] = n
  1437				}
  1438				expr = ast.NewIdent(n.Mangle)
  1439				r.Name = n
  1440				break
  1441			}
  1442		case ctxExpr:
  1443			switch r.Name.Kind {
  1444			case "func":
  1445				if builtinDefs[r.Name.C] != "" {
  1446					error_(r.Pos(), "use of builtin '%s' not in function call", fixGo(r.Name.C))
  1447				}
  1448	
  1449				// Function is being used in an expression, to e.g. pass around a C function pointer.
  1450				// Create a new Name for this Ref which causes the variable to be declared in Go land.
  1451				fpName := "fp_" + r.Name.Go
  1452				name := f.Name[fpName]
  1453				if name == nil {
  1454					name = &Name{
  1455						Go:   fpName,
  1456						C:    r.Name.C,
  1457						Kind: "fpvar",
  1458						Type: &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*"), Go: ast.NewIdent("unsafe.Pointer")},
  1459					}
  1460					p.mangleName(name)
  1461					f.Name[fpName] = name
  1462				}
  1463				r.Name = name
  1464				// Rewrite into call to _Cgo_ptr to prevent assignments. The _Cgo_ptr
  1465				// function is defined in out.go and simply returns its argument. See
  1466				// issue 7757.
  1467				expr = &ast.CallExpr{
  1468					Fun:  &ast.Ident{NamePos: (*r.Expr).Pos(), Name: "_Cgo_ptr"},
  1469					Args: []ast.Expr{ast.NewIdent(name.Mangle)},
  1470				}
  1471			case "type":
  1472				// Okay - might be new(T)
  1473				if r.Name.Type == nil {
  1474					error_(r.Pos(), "expression C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
  1475					break
  1476				}
  1477				expr = r.Name.Type.Go
  1478			case "var":
  1479				expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}
  1480			case "macro":
  1481				expr = &ast.CallExpr{Fun: expr}
  1482			}
  1483		case ctxSelector:
  1484			if r.Name.Kind == "var" {
  1485				expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}
  1486			} else {
  1487				error_(r.Pos(), "only C variables allowed in selector expression %s", fixGo(r.Name.Go))
  1488			}
  1489		case ctxType:
  1490			if r.Name.Kind != "type" {
  1491				error_(r.Pos(), "expression C.%s used as type", fixGo(r.Name.Go))
  1492			} else if r.Name.Type == nil {
  1493				// Use of C.enum_x, C.struct_x or C.union_x without C definition.
  1494				// GCC won't raise an error when using pointers to such unknown types.
  1495				error_(r.Pos(), "type C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
  1496			} else {
  1497				expr = r.Name.Type.Go
  1498			}
  1499		default:
  1500			if r.Name.Kind == "func" {
  1501				error_(r.Pos(), "must call C.%s", fixGo(r.Name.Go))
  1502			}
  1503		}
  1504		return expr
  1505	}
  1506	
  1507	// gofmtPos returns the gofmt-formatted string for an AST node,
  1508	// with a comment setting the position before the node.
  1509	func gofmtPos(n ast.Expr, pos token.Pos) string {
  1510		s := gofmtLine(n)
  1511		p := fset.Position(pos)
  1512		if p.Column == 0 {
  1513			return s
  1514		}
  1515		return fmt.Sprintf("/*line :%d:%d*/%s", p.Line, p.Column, s)
  1516	}
  1517	
  1518	// gccBaseCmd returns the start of the compiler command line.
  1519	// It uses $CC if set, or else $GCC, or else the compiler recorded
  1520	// during the initial build as defaultCC.
  1521	// defaultCC is defined in zdefaultcc.go, written by cmd/dist.
  1522	func (p *Package) gccBaseCmd() []string {
  1523		// Use $CC if set, since that's what the build uses.
  1524		if ret := strings.Fields(os.Getenv("CC")); len(ret) > 0 {
  1525			return ret
  1526		}
  1527		// Try $GCC if set, since that's what we used to use.
  1528		if ret := strings.Fields(os.Getenv("GCC")); len(ret) > 0 {
  1529			return ret
  1530		}
  1531		return strings.Fields(defaultCC(goos, goarch))
  1532	}
  1533	
  1534	// gccMachine returns the gcc -m flag to use, either "-m32", "-m64" or "-marm".
  1535	func (p *Package) gccMachine() []string {
  1536		switch goarch {
  1537		case "amd64":
  1538			return []string{"-m64"}
  1539		case "386":
  1540			return []string{"-m32"}
  1541		case "arm":
  1542			return []string{"-marm"} // not thumb
  1543		case "s390":
  1544			return []string{"-m31"}
  1545		case "s390x":
  1546			return []string{"-m64"}
  1547		case "mips64", "mips64le":
  1548			return []string{"-mabi=64"}
  1549		case "mips", "mipsle":
  1550			return []string{"-mabi=32"}
  1551		}
  1552		return nil
  1553	}
  1554	
  1555	func gccTmp() string {
  1556		return *objDir + "_cgo_.o"
  1557	}
  1558	
  1559	// gccCmd returns the gcc command line to use for compiling
  1560	// the input.
  1561	func (p *Package) gccCmd() []string {
  1562		c := append(p.gccBaseCmd(),
  1563			"-w",          // no warnings
  1564			"-Wno-error",  // warnings are not errors
  1565			"-o"+gccTmp(), // write object to tmp
  1566			"-gdwarf-2",   // generate DWARF v2 debugging symbols
  1567			"-c",          // do not link
  1568			"-xc",         // input language is C
  1569		)
  1570		if p.GccIsClang {
  1571			c = append(c,
  1572				"-ferror-limit=0",
  1573				// Apple clang version 1.7 (tags/Apple/clang-77) (based on LLVM 2.9svn)
  1574				// doesn't have -Wno-unneeded-internal-declaration, so we need yet another
  1575				// flag to disable the warning. Yes, really good diagnostics, clang.
  1576				"-Wno-unknown-warning-option",
  1577				"-Wno-unneeded-internal-declaration",
  1578				"-Wno-unused-function",
  1579				"-Qunused-arguments",
  1580				// Clang embeds prototypes for some builtin functions,
  1581				// like malloc and calloc, but all size_t parameters are
  1582				// incorrectly typed unsigned long. We work around that
  1583				// by disabling the builtin functions (this is safe as
  1584				// it won't affect the actual compilation of the C code).
  1585				// See: https://golang.org/issue/6506.
  1586				"-fno-builtin",
  1587			)
  1588		}
  1589	
  1590		c = append(c, p.GccOptions...)
  1591		c = append(c, p.gccMachine()...)
  1592		if goos == "aix" {
  1593			c = append(c, "-maix64")
  1594			c = append(c, "-mcmodel=large")
  1595		}
  1596		c = append(c, "-") //read input from standard input
  1597		return c
  1598	}
  1599	
  1600	// gccDebug runs gcc -gdwarf-2 over the C program stdin and
  1601	// returns the corresponding DWARF data and, if present, debug data block.
  1602	func (p *Package) gccDebug(stdin []byte, nnames int) (d *dwarf.Data, ints []int64, floats []float64, strs []string) {
  1603		runGcc(stdin, p.gccCmd())
  1604	
  1605		isDebugInts := func(s string) bool {
  1606			// Some systems use leading _ to denote non-assembly symbols.
  1607			return s == "__cgodebug_ints" || s == "___cgodebug_ints"
  1608		}
  1609		isDebugFloats := func(s string) bool {
  1610			// Some systems use leading _ to denote non-assembly symbols.
  1611			return s == "__cgodebug_floats" || s == "___cgodebug_floats"
  1612		}
  1613		indexOfDebugStr := func(s string) int {
  1614			// Some systems use leading _ to denote non-assembly symbols.
  1615			if strings.HasPrefix(s, "___") {
  1616				s = s[1:]
  1617			}
  1618			if strings.HasPrefix(s, "__cgodebug_str__") {
  1619				if n, err := strconv.Atoi(s[len("__cgodebug_str__"):]); err == nil {
  1620					return n
  1621				}
  1622			}
  1623			return -1
  1624		}
  1625		indexOfDebugStrlen := func(s string) int {
  1626			// Some systems use leading _ to denote non-assembly symbols.
  1627			if strings.HasPrefix(s, "___") {
  1628				s = s[1:]
  1629			}
  1630			if strings.HasPrefix(s, "__cgodebug_strlen__") {
  1631				if n, err := strconv.Atoi(s[len("__cgodebug_strlen__"):]); err == nil {
  1632					return n
  1633				}
  1634			}
  1635			return -1
  1636		}
  1637	
  1638		strs = make([]string, nnames)
  1639	
  1640		strdata := make(map[int]string, nnames)
  1641		strlens := make(map[int]int, nnames)
  1642	
  1643		buildStrings := func() {
  1644			for n, strlen := range strlens {
  1645				data := strdata[n]
  1646				if len(data) <= strlen {
  1647					fatalf("invalid string literal")
  1648				}
  1649				strs[n] = data[:strlen]
  1650			}
  1651		}
  1652	
  1653		if f, err := macho.Open(gccTmp()); err == nil {
  1654			defer f.Close()
  1655			d, err := f.DWARF()
  1656			if err != nil {
  1657				fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1658			}
  1659			bo := f.ByteOrder
  1660			if f.Symtab != nil {
  1661				for i := range f.Symtab.Syms {
  1662					s := &f.Symtab.Syms[i]
  1663					switch {
  1664					case isDebugInts(s.Name):
  1665						// Found it. Now find data section.
  1666						if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1667							sect := f.Sections[i]
  1668							if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1669								if sdat, err := sect.Data(); err == nil {
  1670									data := sdat[s.Value-sect.Addr:]
  1671									ints = make([]int64, len(data)/8)
  1672									for i := range ints {
  1673										ints[i] = int64(bo.Uint64(data[i*8:]))
  1674									}
  1675								}
  1676							}
  1677						}
  1678					case isDebugFloats(s.Name):
  1679						// Found it. Now find data section.
  1680						if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1681							sect := f.Sections[i]
  1682							if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1683								if sdat, err := sect.Data(); err == nil {
  1684									data := sdat[s.Value-sect.Addr:]
  1685									floats = make([]float64, len(data)/8)
  1686									for i := range floats {
  1687										floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1688									}
  1689								}
  1690							}
  1691						}
  1692					default:
  1693						if n := indexOfDebugStr(s.Name); n != -1 {
  1694							// Found it. Now find data section.
  1695							if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1696								sect := f.Sections[i]
  1697								if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1698									if sdat, err := sect.Data(); err == nil {
  1699										data := sdat[s.Value-sect.Addr:]
  1700										strdata[n] = string(data)
  1701									}
  1702								}
  1703							}
  1704							break
  1705						}
  1706						if n := indexOfDebugStrlen(s.Name); n != -1 {
  1707							// Found it. Now find data section.
  1708							if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1709								sect := f.Sections[i]
  1710								if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1711									if sdat, err := sect.Data(); err == nil {
  1712										data := sdat[s.Value-sect.Addr:]
  1713										strlen := bo.Uint64(data[:8])
  1714										if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1715											fatalf("string literal too big")
  1716										}
  1717										strlens[n] = int(strlen)
  1718									}
  1719								}
  1720							}
  1721							break
  1722						}
  1723					}
  1724				}
  1725	
  1726				buildStrings()
  1727			}
  1728			return d, ints, floats, strs
  1729		}
  1730	
  1731		if f, err := elf.Open(gccTmp()); err == nil {
  1732			defer f.Close()
  1733			d, err := f.DWARF()
  1734			if err != nil {
  1735				fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1736			}
  1737			bo := f.ByteOrder
  1738			symtab, err := f.Symbols()
  1739			if err == nil {
  1740				for i := range symtab {
  1741					s := &symtab[i]
  1742					switch {
  1743					case isDebugInts(s.Name):
  1744						// Found it. Now find data section.
  1745						if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1746							sect := f.Sections[i]
  1747							if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1748								if sdat, err := sect.Data(); err == nil {
  1749									data := sdat[s.Value-sect.Addr:]
  1750									ints = make([]int64, len(data)/8)
  1751									for i := range ints {
  1752										ints[i] = int64(bo.Uint64(data[i*8:]))
  1753									}
  1754								}
  1755							}
  1756						}
  1757					case isDebugFloats(s.Name):
  1758						// Found it. Now find data section.
  1759						if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1760							sect := f.Sections[i]
  1761							if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1762								if sdat, err := sect.Data(); err == nil {
  1763									data := sdat[s.Value-sect.Addr:]
  1764									floats = make([]float64, len(data)/8)
  1765									for i := range floats {
  1766										floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1767									}
  1768								}
  1769							}
  1770						}
  1771					default:
  1772						if n := indexOfDebugStr(s.Name); n != -1 {
  1773							// Found it. Now find data section.
  1774							if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1775								sect := f.Sections[i]
  1776								if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1777									if sdat, err := sect.Data(); err == nil {
  1778										data := sdat[s.Value-sect.Addr:]
  1779										strdata[n] = string(data)
  1780									}
  1781								}
  1782							}
  1783							break
  1784						}
  1785						if n := indexOfDebugStrlen(s.Name); n != -1 {
  1786							// Found it. Now find data section.
  1787							if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1788								sect := f.Sections[i]
  1789								if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1790									if sdat, err := sect.Data(); err == nil {
  1791										data := sdat[s.Value-sect.Addr:]
  1792										strlen := bo.Uint64(data[:8])
  1793										if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1794											fatalf("string literal too big")
  1795										}
  1796										strlens[n] = int(strlen)
  1797									}
  1798								}
  1799							}
  1800							break
  1801						}
  1802					}
  1803				}
  1804	
  1805				buildStrings()
  1806			}
  1807			return d, ints, floats, strs
  1808		}
  1809	
  1810		if f, err := pe.Open(gccTmp()); err == nil {
  1811			defer f.Close()
  1812			d, err := f.DWARF()
  1813			if err != nil {
  1814				fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1815			}
  1816			bo := binary.LittleEndian
  1817			for _, s := range f.Symbols {
  1818				switch {
  1819				case isDebugInts(s.Name):
  1820					if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1821						sect := f.Sections[i]
  1822						if s.Value < sect.Size {
  1823							if sdat, err := sect.Data(); err == nil {
  1824								data := sdat[s.Value:]
  1825								ints = make([]int64, len(data)/8)
  1826								for i := range ints {
  1827									ints[i] = int64(bo.Uint64(data[i*8:]))
  1828								}
  1829							}
  1830						}
  1831					}
  1832				case isDebugFloats(s.Name):
  1833					if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1834						sect := f.Sections[i]
  1835						if s.Value < sect.Size {
  1836							if sdat, err := sect.Data(); err == nil {
  1837								data := sdat[s.Value:]
  1838								floats = make([]float64, len(data)/8)
  1839								for i := range floats {
  1840									floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1841								}
  1842							}
  1843						}
  1844					}
  1845				default:
  1846					if n := indexOfDebugStr(s.Name); n != -1 {
  1847						if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1848							sect := f.Sections[i]
  1849							if s.Value < sect.Size {
  1850								if sdat, err := sect.Data(); err == nil {
  1851									data := sdat[s.Value:]
  1852									strdata[n] = string(data)
  1853								}
  1854							}
  1855						}
  1856						break
  1857					}
  1858					if n := indexOfDebugStrlen(s.Name); n != -1 {
  1859						if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1860							sect := f.Sections[i]
  1861							if s.Value < sect.Size {
  1862								if sdat, err := sect.Data(); err == nil {
  1863									data := sdat[s.Value:]
  1864									strlen := bo.Uint64(data[:8])
  1865									if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1866										fatalf("string literal too big")
  1867									}
  1868									strlens[n] = int(strlen)
  1869								}
  1870							}
  1871						}
  1872						break
  1873					}
  1874				}
  1875			}
  1876	
  1877			buildStrings()
  1878	
  1879			return d, ints, floats, strs
  1880		}
  1881	
  1882		if f, err := xcoff.Open(gccTmp()); err == nil {
  1883			defer f.Close()
  1884			d, err := f.DWARF()
  1885			if err != nil {
  1886				fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1887			}
  1888			bo := binary.BigEndian
  1889			for _, s := range f.Symbols {
  1890				switch {
  1891				case isDebugInts(s.Name):
  1892					if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1893						sect := f.Sections[i]
  1894						if s.Value < sect.Size {
  1895							if sdat, err := sect.Data(); err == nil {
  1896								data := sdat[s.Value:]
  1897								ints = make([]int64, len(data)/8)
  1898								for i := range ints {
  1899									ints[i] = int64(bo.Uint64(data[i*8:]))
  1900								}
  1901							}
  1902						}
  1903					}
  1904				case isDebugFloats(s.Name):
  1905					if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1906						sect := f.Sections[i]
  1907						if s.Value < sect.Size {
  1908							if sdat, err := sect.Data(); err == nil {
  1909								data := sdat[s.Value:]
  1910								floats = make([]float64, len(data)/8)
  1911								for i := range floats {
  1912									floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1913								}
  1914							}
  1915						}
  1916					}
  1917				default:
  1918					if n := indexOfDebugStr(s.Name); n != -1 {
  1919						if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1920							sect := f.Sections[i]
  1921							if s.Value < sect.Size {
  1922								if sdat, err := sect.Data(); err == nil {
  1923									data := sdat[s.Value:]
  1924									strdata[n] = string(data)
  1925								}
  1926							}
  1927						}
  1928						break
  1929					}
  1930					if n := indexOfDebugStrlen(s.Name); n != -1 {
  1931						if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1932							sect := f.Sections[i]
  1933							if s.Value < sect.Size {
  1934								if sdat, err := sect.Data(); err == nil {
  1935									data := sdat[s.Value:]
  1936									strlen := bo.Uint64(data[:8])
  1937									if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1938										fatalf("string literal too big")
  1939									}
  1940									strlens[n] = int(strlen)
  1941								}
  1942							}
  1943						}
  1944						break
  1945					}
  1946				}
  1947			}
  1948	
  1949			buildStrings()
  1950			return d, ints, floats, strs
  1951		}
  1952		fatalf("cannot parse gcc output %s as ELF, Mach-O, PE, XCOFF object", gccTmp())
  1953		panic("not reached")
  1954	}
  1955	
  1956	// gccDefines runs gcc -E -dM -xc - over the C program stdin
  1957	// and returns the corresponding standard output, which is the
  1958	// #defines that gcc encountered while processing the input
  1959	// and its included files.
  1960	func (p *Package) gccDefines(stdin []byte) string {
  1961		base := append(p.gccBaseCmd(), "-E", "-dM", "-xc")
  1962		base = append(base, p.gccMachine()...)
  1963		stdout, _ := runGcc(stdin, append(append(base, p.GccOptions...), "-"))
  1964		return stdout
  1965	}
  1966	
  1967	// gccErrors runs gcc over the C program stdin and returns
  1968	// the errors that gcc prints. That is, this function expects
  1969	// gcc to fail.
  1970	func (p *Package) gccErrors(stdin []byte) string {
  1971		// TODO(rsc): require failure
  1972		args := p.gccCmd()
  1973	
  1974		// Optimization options can confuse the error messages; remove them.
  1975		nargs := make([]string, 0, len(args))
  1976		for _, arg := range args {
  1977			if !strings.HasPrefix(arg, "-O") {
  1978				nargs = append(nargs, arg)
  1979			}
  1980		}
  1981	
  1982		// Force -O0 optimization but keep the trailing "-" at the end.
  1983		nargs = append(nargs, "-O0")
  1984		nl := len(nargs)
  1985		nargs[nl-2], nargs[nl-1] = nargs[nl-1], nargs[nl-2]
  1986	
  1987		if *debugGcc {
  1988			fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(nargs, " "))
  1989			os.Stderr.Write(stdin)
  1990			fmt.Fprint(os.Stderr, "EOF\n")
  1991		}
  1992		stdout, stderr, _ := run(stdin, nargs)
  1993		if *debugGcc {
  1994			os.Stderr.Write(stdout)
  1995			os.Stderr.Write(stderr)
  1996		}
  1997		return string(stderr)
  1998	}
  1999	
  2000	// runGcc runs the gcc command line args with stdin on standard input.
  2001	// If the command exits with a non-zero exit status, runGcc prints
  2002	// details about what was run and exits.
  2003	// Otherwise runGcc returns the data written to standard output and standard error.
  2004	// Note that for some of the uses we expect useful data back
  2005	// on standard error, but for those uses gcc must still exit 0.
  2006	func runGcc(stdin []byte, args []string) (string, string) {
  2007		if *debugGcc {
  2008			fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(args, " "))
  2009			os.Stderr.Write(stdin)
  2010			fmt.Fprint(os.Stderr, "EOF\n")
  2011		}
  2012		stdout, stderr, ok := run(stdin, args)
  2013		if *debugGcc {
  2014			os.Stderr.Write(stdout)
  2015			os.Stderr.Write(stderr)
  2016		}
  2017		if !ok {
  2018			os.Stderr.Write(stderr)
  2019			os.Exit(2)
  2020		}
  2021		return string(stdout), string(stderr)
  2022	}
  2023	
  2024	// A typeConv is a translator from dwarf types to Go types
  2025	// with equivalent memory layout.
  2026	type typeConv struct {
  2027		// Cache of already-translated or in-progress types.
  2028		m map[string]*Type
  2029	
  2030		// Map from types to incomplete pointers to those types.
  2031		ptrs map[string][]*Type
  2032		// Keys of ptrs in insertion order (deterministic worklist)
  2033		// ptrKeys contains exactly the keys in ptrs.
  2034		ptrKeys []dwarf.Type
  2035	
  2036		// Type names X for which there exists an XGetTypeID function with type func() CFTypeID.
  2037		getTypeIDs map[string]bool
  2038	
  2039		// Predeclared types.
  2040		bool                                   ast.Expr
  2041		byte                                   ast.Expr // denotes padding
  2042		int8, int16, int32, int64              ast.Expr
  2043		uint8, uint16, uint32, uint64, uintptr ast.Expr
  2044		float32, float64                       ast.Expr
  2045		complex64, complex128                  ast.Expr
  2046		void                                   ast.Expr
  2047		string                                 ast.Expr
  2048		goVoid                                 ast.Expr // _Ctype_void, denotes C's void
  2049		goVoidPtr                              ast.Expr // unsafe.Pointer or *byte
  2050	
  2051		ptrSize int64
  2052		intSize int64
  2053	}
  2054	
  2055	var tagGen int
  2056	var typedef = make(map[string]*Type)
  2057	var goIdent = make(map[string]*ast.Ident)
  2058	
  2059	// unionWithPointer is true for a Go type that represents a C union (or class)
  2060	// that may contain a pointer. This is used for cgo pointer checking.
  2061	var unionWithPointer = make(map[ast.Expr]bool)
  2062	
  2063	func (c *typeConv) Init(ptrSize, intSize int64) {
  2064		c.ptrSize = ptrSize
  2065		c.intSize = intSize
  2066		c.m = make(map[string]*Type)
  2067		c.ptrs = make(map[string][]*Type)
  2068		c.getTypeIDs = make(map[string]bool)
  2069		c.bool = c.Ident("bool")
  2070		c.byte = c.Ident("byte")
  2071		c.int8 = c.Ident("int8")
  2072		c.int16 = c.Ident("int16")
  2073		c.int32 = c.Ident("int32")
  2074		c.int64 = c.Ident("int64")
  2075		c.uint8 = c.Ident("uint8")
  2076		c.uint16 = c.Ident("uint16")
  2077		c.uint32 = c.Ident("uint32")
  2078		c.uint64 = c.Ident("uint64")
  2079		c.uintptr = c.Ident("uintptr")
  2080		c.float32 = c.Ident("float32")
  2081		c.float64 = c.Ident("float64")
  2082		c.complex64 = c.Ident("complex64")
  2083		c.complex128 = c.Ident("complex128")
  2084		c.void = c.Ident("void")
  2085		c.string = c.Ident("string")
  2086		c.goVoid = c.Ident("_Ctype_void")
  2087	
  2088		// Normally cgo translates void* to unsafe.Pointer,
  2089		// but for historical reasons -godefs uses *byte instead.
  2090		if *godefs {
  2091			c.goVoidPtr = &ast.StarExpr{X: c.byte}
  2092		} else {
  2093			c.goVoidPtr = c.Ident("unsafe.Pointer")
  2094		}
  2095	}
  2096	
  2097	// base strips away qualifiers and typedefs to get the underlying type
  2098	func base(dt dwarf.Type) dwarf.Type {
  2099		for {
  2100			if d, ok := dt.(*dwarf.QualType); ok {
  2101				dt = d.Type
  2102				continue
  2103			}
  2104			if d, ok := dt.(*dwarf.TypedefType); ok {
  2105				dt = d.Type
  2106				continue
  2107			}
  2108			break
  2109		}
  2110		return dt
  2111	}
  2112	
  2113	// unqual strips away qualifiers from a DWARF type.
  2114	// In general we don't care about top-level qualifiers.
  2115	func unqual(dt dwarf.Type) dwarf.Type {
  2116		for {
  2117			if d, ok := dt.(*dwarf.QualType); ok {
  2118				dt = d.Type
  2119			} else {
  2120				break
  2121			}
  2122		}
  2123		return dt
  2124	}
  2125	
  2126	// Map from dwarf text names to aliases we use in package "C".
  2127	var dwarfToName = map[string]string{
  2128		"long int":               "long",
  2129		"long unsigned int":      "ulong",
  2130		"unsigned int":           "uint",
  2131		"short unsigned int":     "ushort",
  2132		"unsigned short":         "ushort", // Used by Clang; issue 13129.
  2133		"short int":              "short",
  2134		"long long int":          "longlong",
  2135		"long long unsigned int": "ulonglong",
  2136		"signed char":            "schar",
  2137		"unsigned char":          "uchar",
  2138	}
  2139	
  2140	const signedDelta = 64
  2141	
  2142	// String returns the current type representation. Format arguments
  2143	// are assembled within this method so that any changes in mutable
  2144	// values are taken into account.
  2145	func (tr *TypeRepr) String() string {
  2146		if len(tr.Repr) == 0 {
  2147			return ""
  2148		}
  2149		if len(tr.FormatArgs) == 0 {
  2150			return tr.Repr
  2151		}
  2152		return fmt.Sprintf(tr.Repr, tr.FormatArgs...)
  2153	}
  2154	
  2155	// Empty reports whether the result of String would be "".
  2156	func (tr *TypeRepr) Empty() bool {
  2157		return len(tr.Repr) == 0
  2158	}
  2159	
  2160	// Set modifies the type representation.
  2161	// If fargs are provided, repr is used as a format for fmt.Sprintf.
  2162	// Otherwise, repr is used unprocessed as the type representation.
  2163	func (tr *TypeRepr) Set(repr string, fargs ...interface{}) {
  2164		tr.Repr = repr
  2165		tr.FormatArgs = fargs
  2166	}
  2167	
  2168	// FinishType completes any outstanding type mapping work.
  2169	// In particular, it resolves incomplete pointer types.
  2170	func (c *typeConv) FinishType(pos token.Pos) {
  2171		// Completing one pointer type might produce more to complete.
  2172		// Keep looping until they're all done.
  2173		for len(c.ptrKeys) > 0 {
  2174			dtype := c.ptrKeys[0]
  2175			dtypeKey := dtype.String()
  2176			c.ptrKeys = c.ptrKeys[1:]
  2177			ptrs := c.ptrs[dtypeKey]
  2178			delete(c.ptrs, dtypeKey)
  2179	
  2180			// Note Type might invalidate c.ptrs[dtypeKey].
  2181			t := c.Type(dtype, pos)
  2182			for _, ptr := range ptrs {
  2183				ptr.Go.(*ast.StarExpr).X = t.Go
  2184				ptr.C.Set("%s*", t.C)
  2185			}
  2186		}
  2187	}
  2188	
  2189	// Type returns a *Type with the same memory layout as
  2190	// dtype when used as the type of a variable or a struct field.
  2191	func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
  2192		// Always recompute bad pointer typedefs, as the set of such
  2193		// typedefs changes as we see more types.
  2194		checkCache := true
  2195		if dtt, ok := dtype.(*dwarf.TypedefType); ok && c.badPointerTypedef(dtt) {
  2196			checkCache = false
  2197		}
  2198	
  2199		key := dtype.String()
  2200	
  2201		if checkCache {
  2202			if t, ok := c.m[key]; ok {
  2203				if t.Go == nil {
  2204					fatalf("%s: type conversion loop at %s", lineno(pos), dtype)
  2205				}
  2206				return t
  2207			}
  2208		}
  2209	
  2210		t := new(Type)
  2211		t.Size = dtype.Size() // note: wrong for array of pointers, corrected below
  2212		t.Align = -1
  2213		t.C = &TypeRepr{Repr: dtype.Common().Name}
  2214		c.m[key] = t
  2215	
  2216		switch dt := dtype.(type) {
  2217		default:
  2218			fatalf("%s: unexpected type: %s", lineno(pos), dtype)
  2219	
  2220		case *dwarf.AddrType:
  2221			if t.Size != c.ptrSize {
  2222				fatalf("%s: unexpected: %d-byte address type - %s", lineno(pos), t.Size, dtype)
  2223			}
  2224			t.Go = c.uintptr
  2225			t.Align = t.Size
  2226	
  2227		case *dwarf.ArrayType:
  2228			if dt.StrideBitSize > 0 {
  2229				// Cannot represent bit-sized elements in Go.
  2230				t.Go = c.Opaque(t.Size)
  2231				break
  2232			}
  2233			count := dt.Count
  2234			if count == -1 {
  2235				// Indicates flexible array member, which Go doesn't support.
  2236				// Translate to zero-length array instead.
  2237				count = 0
  2238			}
  2239			sub := c.Type(dt.Type, pos)
  2240			t.Align = sub.Align
  2241			t.Go = &ast.ArrayType{
  2242				Len: c.intExpr(count),
  2243				Elt: sub.Go,
  2244			}
  2245			// Recalculate t.Size now that we know sub.Size.
  2246			t.Size = count * sub.Size
  2247			t.C.Set("__typeof__(%s[%d])", sub.C, dt.Count)
  2248	
  2249		case *dwarf.BoolType:
  2250			t.Go = c.bool
  2251			t.Align = 1
  2252	
  2253		case *dwarf.CharType:
  2254			if t.Size != 1 {
  2255				fatalf("%s: unexpected: %d-byte char type - %s", lineno(pos), t.Size, dtype)
  2256			}
  2257			t.Go = c.int8
  2258			t.Align = 1
  2259	
  2260		case *dwarf.EnumType:
  2261			if t.Align = t.Size; t.Align >= c.ptrSize {
  2262				t.Align = c.ptrSize
  2263			}
  2264			t.C.Set("enum " + dt.EnumName)
  2265			signed := 0
  2266			t.EnumValues = make(map[string]int64)
  2267			for _, ev := range dt.Val {
  2268				t.EnumValues[ev.Name] = ev.Val
  2269				if ev.Val < 0 {
  2270					signed = signedDelta
  2271				}
  2272			}
  2273			switch t.Size + int64(signed) {
  2274			default:
  2275				fatalf("%s: unexpected: %d-byte enum type - %s", lineno(pos), t.Size, dtype)
  2276			case 1:
  2277				t.Go = c.uint8
  2278			case 2:
  2279				t.Go = c.uint16
  2280			case 4:
  2281				t.Go = c.uint32
  2282			case 8:
  2283				t.Go = c.uint64
  2284			case 1 + signedDelta:
  2285				t.Go = c.int8
  2286			case 2 + signedDelta:
  2287				t.Go = c.int16
  2288			case 4 + signedDelta:
  2289				t.Go = c.int32
  2290			case 8 + signedDelta:
  2291				t.Go = c.int64
  2292			}
  2293	
  2294		case *dwarf.FloatType:
  2295			switch t.Size {
  2296			default:
  2297				fatalf("%s: unexpected: %d-byte float type - %s", lineno(pos), t.Size, dtype)
  2298			case 4:
  2299				t.Go = c.float32
  2300			case 8:
  2301				t.Go = c.float64
  2302			}
  2303			if t.Align = t.Size; t.Align >= c.ptrSize {
  2304				t.Align = c.ptrSize
  2305			}
  2306	
  2307		case *dwarf.ComplexType:
  2308			switch t.Size {
  2309			default:
  2310				fatalf("%s: unexpected: %d-byte complex type - %s", lineno(pos), t.Size, dtype)
  2311			case 8:
  2312				t.Go = c.complex64
  2313			case 16:
  2314				t.Go = c.complex128
  2315			}
  2316			if t.Align = t.Size / 2; t.Align >= c.ptrSize {
  2317				t.Align = c.ptrSize
  2318			}
  2319	
  2320		case *dwarf.FuncType:
  2321			// No attempt at translation: would enable calls
  2322			// directly between worlds, but we need to moderate those.
  2323			t.Go = c.uintptr
  2324			t.Align = c.ptrSize
  2325	
  2326		case *dwarf.IntType:
  2327			if dt.BitSize > 0 {
  2328				fatalf("%s: unexpected: %d-bit int type - %s", lineno(pos), dt.BitSize, dtype)
  2329			}
  2330			switch t.Size {
  2331			default:
  2332				fatalf("%s: unexpected: %d-byte int type - %s", lineno(pos), t.Size, dtype)
  2333			case 1:
  2334				t.Go = c.int8
  2335			case 2:
  2336				t.Go = c.int16
  2337			case 4:
  2338				t.Go = c.int32
  2339			case 8:
  2340				t.Go = c.int64
  2341			case 16:
  2342				t.Go = &ast.ArrayType{
  2343					Len: c.intExpr(t.Size),
  2344					Elt: c.uint8,
  2345				}
  2346			}
  2347			if t.Align = t.Size; t.Align >= c.ptrSize {
  2348				t.Align = c.ptrSize
  2349			}
  2350	
  2351		case *dwarf.PtrType:
  2352			// Clang doesn't emit DW_AT_byte_size for pointer types.
  2353			if t.Size != c.ptrSize && t.Size != -1 {
  2354				fatalf("%s: unexpected: %d-byte pointer type - %s", lineno(pos), t.Size, dtype)
  2355			}
  2356			t.Size = c.ptrSize
  2357			t.Align = c.ptrSize
  2358	
  2359			if _, ok := base(dt.Type).(*dwarf.VoidType); ok {
  2360				t.Go = c.goVoidPtr
  2361				t.C.Set("void*")
  2362				dq := dt.Type
  2363				for {
  2364					if d, ok := dq.(*dwarf.QualType); ok {
  2365						t.C.Set(d.Qual + " " + t.C.String())
  2366						dq = d.Type
  2367					} else {
  2368						break
  2369					}
  2370				}
  2371				break
  2372			}
  2373	
  2374			// Placeholder initialization; completed in FinishType.
  2375			t.Go = &ast.StarExpr{}
  2376			t.C.Set("<incomplete>*")
  2377			key := dt.Type.String()
  2378			if _, ok := c.ptrs[key]; !ok {
  2379				c.ptrKeys = append(c.ptrKeys, dt.Type)
  2380			}
  2381			c.ptrs[key] = append(c.ptrs[key], t)
  2382	
  2383		case *dwarf.QualType:
  2384			t1 := c.Type(dt.Type, pos)
  2385			t.Size = t1.Size
  2386			t.Align = t1.Align
  2387			t.Go = t1.Go
  2388			if unionWithPointer[t1.Go] {
  2389				unionWithPointer[t.Go] = true
  2390			}
  2391			t.EnumValues = nil
  2392			t.Typedef = ""
  2393			t.C.Set("%s "+dt.Qual, t1.C)
  2394			return t
  2395	
  2396		case *dwarf.StructType:
  2397			// Convert to Go struct, being careful about alignment.
  2398			// Have to give it a name to simulate C "struct foo" references.
  2399			tag := dt.StructName
  2400			if dt.ByteSize < 0 && tag == "" { // opaque unnamed struct - should not be possible
  2401				break
  2402			}
  2403			if tag == "" {
  2404				tag = "__" + strconv.Itoa(tagGen)
  2405				tagGen++
  2406			} else if t.C.Empty() {
  2407				t.C.Set(dt.Kind + " " + tag)
  2408			}
  2409			name := c.Ident("_Ctype_" + dt.Kind + "_" + tag)
  2410			t.Go = name // publish before recursive calls
  2411			goIdent[name.Name] = name
  2412			if dt.ByteSize < 0 {
  2413				// Size calculation in c.Struct/c.Opaque will die with size=-1 (unknown),
  2414				// so execute the basic things that the struct case would do
  2415				// other than try to determine a Go representation.
  2416				tt := *t
  2417				tt.C = &TypeRepr{"%s %s", []interface{}{dt.Kind, tag}}
  2418				tt.Go = c.Ident("struct{}")
  2419				typedef[name.Name] = &tt
  2420				break
  2421			}
  2422			switch dt.Kind {
  2423			case "class", "union":
  2424				t.Go = c.Opaque(t.Size)
  2425				if c.dwarfHasPointer(dt, pos) {
  2426					unionWithPointer[t.Go] = true
  2427				}
  2428				if t.C.Empty() {
  2429					t.C.Set("__typeof__(unsigned char[%d])", t.Size)
  2430				}
  2431				t.Align = 1 // TODO: should probably base this on field alignment.
  2432				typedef[name.Name] = t
  2433			case "struct":
  2434				g, csyntax, align := c.Struct(dt, pos)
  2435				if t.C.Empty() {
  2436					t.C.Set(csyntax)
  2437				}
  2438				t.Align = align
  2439				tt := *t
  2440				if tag != "" {
  2441					tt.C = &TypeRepr{"struct %s", []interface{}{tag}}
  2442				}
  2443				tt.Go = g
  2444				typedef[name.Name] = &tt
  2445			}
  2446	
  2447		case *dwarf.TypedefType:
  2448			// Record typedef for printing.
  2449			if dt.Name == "_GoString_" {
  2450				// Special C name for Go string type.
  2451				// Knows string layout used by compilers: pointer plus length,
  2452				// which rounds up to 2 pointers after alignment.
  2453				t.Go = c.string
  2454				t.Size = c.ptrSize * 2
  2455				t.Align = c.ptrSize
  2456				break
  2457			}
  2458			if dt.Name == "_GoBytes_" {
  2459				// Special C name for Go []byte type.
  2460				// Knows slice layout used by compilers: pointer, length, cap.
  2461				t.Go = c.Ident("[]byte")
  2462				t.Size = c.ptrSize + 4 + 4
  2463				t.Align = c.ptrSize
  2464				break
  2465			}
  2466			name := c.Ident("_Ctype_" + dt.Name)
  2467			goIdent[name.Name] = name
  2468			sub := c.Type(dt.Type, pos)
  2469			if c.badPointerTypedef(dt) {
  2470				// Treat this typedef as a uintptr.
  2471				s := *sub
  2472				s.Go = c.uintptr
  2473				s.BadPointer = true
  2474				sub = &s
  2475				// Make sure we update any previously computed type.
  2476				if oldType := typedef[name.Name]; oldType != nil {
  2477					oldType.Go = sub.Go
  2478					oldType.BadPointer = true
  2479				}
  2480			}
  2481			t.Go = name
  2482			t.BadPointer = sub.BadPointer
  2483			if unionWithPointer[sub.Go] {
  2484				unionWithPointer[t.Go] = true
  2485			}
  2486			t.Size = sub.Size
  2487			t.Align = sub.Align
  2488			oldType := typedef[name.Name]
  2489			if oldType == nil {
  2490				tt := *t
  2491				tt.Go = sub.Go
  2492				tt.BadPointer = sub.BadPointer
  2493				typedef[name.Name] = &tt
  2494			}
  2495	
  2496			// If sub.Go.Name is "_Ctype_struct_foo" or "_Ctype_union_foo" or "_Ctype_class_foo",
  2497			// use that as the Go form for this typedef too, so that the typedef will be interchangeable
  2498			// with the base type.
  2499			// In -godefs mode, do this for all typedefs.
  2500			if isStructUnionClass(sub.Go) || *godefs {
  2501				t.Go = sub.Go
  2502	
  2503				if isStructUnionClass(sub.Go) {
  2504					// Use the typedef name for C code.
  2505					typedef[sub.Go.(*ast.Ident).Name].C = t.C
  2506				}
  2507	
  2508				// If we've seen this typedef before, and it
  2509				// was an anonymous struct/union/class before
  2510				// too, use the old definition.
  2511				// TODO: it would be safer to only do this if
  2512				// we verify that the types are the same.
  2513				if oldType != nil && isStructUnionClass(oldType.Go) {
  2514					t.Go = oldType.Go
  2515				}
  2516			}
  2517	
  2518		case *dwarf.UcharType:
  2519			if t.Size != 1 {
  2520				fatalf("%s: unexpected: %d-byte uchar type - %s", lineno(pos), t.Size, dtype)
  2521			}
  2522			t.Go = c.uint8
  2523			t.Align = 1
  2524	
  2525		case *dwarf.UintType:
  2526			if dt.BitSize > 0 {
  2527				fatalf("%s: unexpected: %d-bit uint type - %s", lineno(pos), dt.BitSize, dtype)
  2528			}
  2529			switch t.Size {
  2530			default:
  2531				fatalf("%s: unexpected: %d-byte uint type - %s", lineno(pos), t.Size, dtype)
  2532			case 1:
  2533				t.Go = c.uint8
  2534			case 2:
  2535				t.Go = c.uint16
  2536			case 4:
  2537				t.Go = c.uint32
  2538			case 8:
  2539				t.Go = c.uint64
  2540			case 16:
  2541				t.Go = &ast.ArrayType{
  2542					Len: c.intExpr(t.Size),
  2543					Elt: c.uint8,
  2544				}
  2545			}
  2546			if t.Align = t.Size; t.Align >= c.ptrSize {
  2547				t.Align = c.ptrSize
  2548			}
  2549	
  2550		case *dwarf.VoidType:
  2551			t.Go = c.goVoid
  2552			t.C.Set("void")
  2553			t.Align = 1
  2554		}
  2555	
  2556		switch dtype.(type) {
  2557		case *dwarf.AddrType, *dwarf.BoolType, *dwarf.CharType, *dwarf.ComplexType, *dwarf.IntType, *dwarf.FloatType, *dwarf.UcharType, *dwarf.UintType:
  2558			s := dtype.Common().Name
  2559			if s != "" {
  2560				if ss, ok := dwarfToName[s]; ok {
  2561					s = ss
  2562				}
  2563				s = strings.Replace(s, " ", "", -1)
  2564				name := c.Ident("_Ctype_" + s)
  2565				tt := *t
  2566				typedef[name.Name] = &tt
  2567				if !*godefs {
  2568					t.Go = name
  2569				}
  2570			}
  2571		}
  2572	
  2573		if t.Size < 0 {
  2574			// Unsized types are [0]byte, unless they're typedefs of other types
  2575			// or structs with tags.
  2576			// if so, use the name we've already defined.
  2577			t.Size = 0
  2578			switch dt := dtype.(type) {
  2579			case *dwarf.TypedefType:
  2580				// ok
  2581			case *dwarf.StructType:
  2582				if dt.StructName != "" {
  2583					break
  2584				}
  2585				t.Go = c.Opaque(0)
  2586			default:
  2587				t.Go = c.Opaque(0)
  2588			}
  2589			if t.C.Empty() {
  2590				t.C.Set("void")
  2591			}
  2592		}
  2593	
  2594		if t.C.Empty() {
  2595			fatalf("%s: internal error: did not create C name for %s", lineno(pos), dtype)
  2596		}
  2597	
  2598		return t
  2599	}
  2600	
  2601	// isStructUnionClass reports whether the type described by the Go syntax x
  2602	// is a struct, union, or class with a tag.
  2603	func isStructUnionClass(x ast.Expr) bool {
  2604		id, ok := x.(*ast.Ident)
  2605		if !ok {
  2606			return false
  2607		}
  2608		name := id.Name
  2609		return strings.HasPrefix(name, "_Ctype_struct_") ||
  2610			strings.HasPrefix(name, "_Ctype_union_") ||
  2611			strings.HasPrefix(name, "_Ctype_class_")
  2612	}
  2613	
  2614	// FuncArg returns a Go type with the same memory layout as
  2615	// dtype when used as the type of a C function argument.
  2616	func (c *typeConv) FuncArg(dtype dwarf.Type, pos token.Pos) *Type {
  2617		t := c.Type(unqual(dtype), pos)
  2618		switch dt := dtype.(type) {
  2619		case *dwarf.ArrayType:
  2620			// Arrays are passed implicitly as pointers in C.
  2621			// In Go, we must be explicit.
  2622			tr := &TypeRepr{}
  2623			tr.Set("%s*", t.C)
  2624			return &Type{
  2625				Size:  c.ptrSize,
  2626				Align: c.ptrSize,
  2627				Go:    &ast.StarExpr{X: t.Go},
  2628				C:     tr,
  2629			}
  2630		case *dwarf.TypedefType:
  2631			// C has much more relaxed rules than Go for
  2632			// implicit type conversions. When the parameter
  2633			// is type T defined as *X, simulate a little of the
  2634			// laxness of C by making the argument *X instead of T.
  2635			if ptr, ok := base(dt.Type).(*dwarf.PtrType); ok {
  2636				// Unless the typedef happens to point to void* since
  2637				// Go has special rules around using unsafe.Pointer.
  2638				if _, void := base(ptr.Type).(*dwarf.VoidType); void {
  2639					break
  2640				}
  2641				// ...or the typedef is one in which we expect bad pointers.
  2642				// It will be a uintptr instead of *X.
  2643				if c.baseBadPointerTypedef(dt) {
  2644					break
  2645				}
  2646	
  2647				t = c.Type(ptr, pos)
  2648				if t == nil {
  2649					return nil
  2650				}
  2651	
  2652				// For a struct/union/class, remember the C spelling,
  2653				// in case it has __attribute__((unavailable)).
  2654				// See issue 2888.
  2655				if isStructUnionClass(t.Go) {
  2656					t.Typedef = dt.Name
  2657				}
  2658			}
  2659		}
  2660		return t
  2661	}
  2662	
  2663	// FuncType returns the Go type analogous to dtype.
  2664	// There is no guarantee about matching memory layout.
  2665	func (c *typeConv) FuncType(dtype *dwarf.FuncType, pos token.Pos) *FuncType {
  2666		p := make([]*Type, len(dtype.ParamType))
  2667		gp := make([]*ast.Field, len(dtype.ParamType))
  2668		for i, f := range dtype.ParamType {
  2669			// gcc's DWARF generator outputs a single DotDotDotType parameter for
  2670			// function pointers that specify no parameters (e.g. void
  2671			// (*__cgo_0)()).  Treat this special case as void. This case is
  2672			// invalid according to ISO C anyway (i.e. void (*__cgo_1)(...) is not
  2673			// legal).
  2674			if _, ok := f.(*dwarf.DotDotDotType); ok && i == 0 {
  2675				p, gp = nil, nil
  2676				break
  2677			}
  2678			p[i] = c.FuncArg(f, pos)
  2679			gp[i] = &ast.Field{Type: p[i].Go}
  2680		}
  2681		var r *Type
  2682		var gr []*ast.Field
  2683		if _, ok := base(dtype.ReturnType).(*dwarf.VoidType); ok {
  2684			gr = []*ast.Field{{Type: c.goVoid}}
  2685		} else if dtype.ReturnType != nil {
  2686			r = c.Type(unqual(dtype.ReturnType), pos)
  2687			gr = []*ast.Field{{Type: r.Go}}
  2688		}
  2689		return &FuncType{
  2690			Params: p,
  2691			Result: r,
  2692			Go: &ast.FuncType{
  2693				Params:  &ast.FieldList{List: gp},
  2694				Results: &ast.FieldList{List: gr},
  2695			},
  2696		}
  2697	}
  2698	
  2699	// Identifier
  2700	func (c *typeConv) Ident(s string) *ast.Ident {
  2701		return ast.NewIdent(s)
  2702	}
  2703	
  2704	// Opaque type of n bytes.
  2705	func (c *typeConv) Opaque(n int64) ast.Expr {
  2706		return &ast.ArrayType{
  2707			Len: c.intExpr(n),
  2708			Elt: c.byte,
  2709		}
  2710	}
  2711	
  2712	// Expr for integer n.
  2713	func (c *typeConv) intExpr(n int64) ast.Expr {
  2714		return &ast.BasicLit{
  2715			Kind:  token.INT,
  2716			Value: strconv.FormatInt(n, 10),
  2717		}
  2718	}
  2719	
  2720	// Add padding of given size to fld.
  2721	func (c *typeConv) pad(fld []*ast.Field, sizes []int64, size int64) ([]*ast.Field, []int64) {
  2722		n := len(fld)
  2723		fld = fld[0 : n+1]
  2724		fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident("_")}, Type: c.Opaque(size)}
  2725		sizes = sizes[0 : n+1]
  2726		sizes[n] = size
  2727		return fld, sizes
  2728	}
  2729	
  2730	// Struct conversion: return Go and (gc) C syntax for type.
  2731	func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.StructType, csyntax string, align int64) {
  2732		// Minimum alignment for a struct is 1 byte.
  2733		align = 1
  2734	
  2735		var buf bytes.Buffer
  2736		buf.WriteString("struct {")
  2737		fld := make([]*ast.Field, 0, 2*len(dt.Field)+1) // enough for padding around every field
  2738		sizes := make([]int64, 0, 2*len(dt.Field)+1)
  2739		off := int64(0)
  2740	
  2741		// Rename struct fields that happen to be named Go keywords into
  2742		// _{keyword}.  Create a map from C ident -> Go ident. The Go ident will
  2743		// be mangled. Any existing identifier that already has the same name on
  2744		// the C-side will cause the Go-mangled version to be prefixed with _.
  2745		// (e.g. in a struct with fields '_type' and 'type', the latter would be
  2746		// rendered as '__type' in Go).
  2747		ident := make(map[string]string)
  2748		used := make(map[string]bool)
  2749		for _, f := range dt.Field {
  2750			ident[f.Name] = f.Name
  2751			used[f.Name] = true
  2752		}
  2753	
  2754		if !*godefs {
  2755			for cid, goid := range ident {
  2756				if token.Lookup(goid).IsKeyword() {
  2757					// Avoid keyword
  2758					goid = "_" + goid
  2759	
  2760					// Also avoid existing fields
  2761					for _, exist := used[goid]; exist; _, exist = used[goid] {
  2762						goid = "_" + goid
  2763					}
  2764	
  2765					used[goid] = true
  2766					ident[cid] = goid
  2767				}
  2768			}
  2769		}
  2770	
  2771		anon := 0
  2772		for _, f := range dt.Field {
  2773			name := f.Name
  2774			ft := f.Type
  2775	
  2776			// In godefs mode, if this field is a C11
  2777			// anonymous union then treat the first field in the
  2778			// union as the field in the struct. This handles
  2779			// cases like the glibc <sys/resource.h> file; see
  2780			// issue 6677.
  2781			if *godefs {
  2782				if st, ok := f.Type.(*dwarf.StructType); ok && name == "" && st.Kind == "union" && len(st.Field) > 0 && !used[st.Field[0].Name] {
  2783					name = st.Field[0].Name
  2784					ident[name] = name
  2785					ft = st.Field[0].Type
  2786				}
  2787			}
  2788	
  2789			// TODO: Handle fields that are anonymous structs by
  2790			// promoting the fields of the inner struct.
  2791	
  2792			t := c.Type(ft, pos)
  2793			tgo := t.Go
  2794			size := t.Size
  2795			talign := t.Align
  2796			if f.BitSize > 0 {
  2797				switch f.BitSize {
  2798				case 8, 16, 32, 64:
  2799				default:
  2800					continue
  2801				}
  2802				size = f.BitSize / 8
  2803				name := tgo.(*ast.Ident).String()
  2804				if strings.HasPrefix(name, "int") {
  2805					name = "int"
  2806				} else {
  2807					name = "uint"
  2808				}
  2809				tgo = ast.NewIdent(name + fmt.Sprint(f.BitSize))
  2810				talign = size
  2811			}
  2812	
  2813			if talign > 0 && f.ByteOffset%talign != 0 {
  2814				// Drop misaligned fields, the same way we drop integer bit fields.
  2815				// The goal is to make available what can be made available.
  2816				// Otherwise one bad and unneeded field in an otherwise okay struct
  2817				// makes the whole program not compile. Much of the time these
  2818				// structs are in system headers that cannot be corrected.
  2819				continue
  2820			}
  2821	
  2822			// Round off up to talign, assumed to be a power of 2.
  2823			off = (off + talign - 1) &^ (talign - 1)
  2824	
  2825			if f.ByteOffset > off {
  2826				fld, sizes = c.pad(fld, sizes, f.ByteOffset-off)
  2827				off = f.ByteOffset
  2828			}
  2829			if f.ByteOffset < off {
  2830				// Drop a packed field that we can't represent.
  2831				continue
  2832			}
  2833	
  2834			n := len(fld)
  2835			fld = fld[0 : n+1]
  2836			if name == "" {
  2837				name = fmt.Sprintf("anon%d", anon)
  2838				anon++
  2839				ident[name] = name
  2840			}
  2841			fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[name])}, Type: tgo}
  2842			sizes = sizes[0 : n+1]
  2843			sizes[n] = size
  2844			off += size
  2845			buf.WriteString(t.C.String())
  2846			buf.WriteString(" ")
  2847			buf.WriteString(name)
  2848			buf.WriteString("; ")
  2849			if talign > align {
  2850				align = talign
  2851			}
  2852		}
  2853		if off < dt.ByteSize {
  2854			fld, sizes = c.pad(fld, sizes, dt.ByteSize-off)
  2855			off = dt.ByteSize
  2856		}
  2857	
  2858		// If the last field in a non-zero-sized struct is zero-sized
  2859		// the compiler is going to pad it by one (see issue 9401).
  2860		// We can't permit that, because then the size of the Go
  2861		// struct will not be the same as the size of the C struct.
  2862		// Our only option in such a case is to remove the field,
  2863		// which means that it cannot be referenced from Go.
  2864		for off > 0 && sizes[len(sizes)-1] == 0 {
  2865			n := len(sizes)
  2866			fld = fld[0 : n-1]
  2867			sizes = sizes[0 : n-1]
  2868		}
  2869	
  2870		if off != dt.ByteSize {
  2871			fatalf("%s: struct size calculation error off=%d bytesize=%d", lineno(pos), off, dt.ByteSize)
  2872		}
  2873		buf.WriteString("}")
  2874		csyntax = buf.String()
  2875	
  2876		if *godefs {
  2877			godefsFields(fld)
  2878		}
  2879		expr = &ast.StructType{Fields: &ast.FieldList{List: fld}}
  2880		return
  2881	}
  2882	
  2883	// dwarfHasPointer reports whether the DWARF type dt contains a pointer.
  2884	func (c *typeConv) dwarfHasPointer(dt dwarf.Type, pos token.Pos) bool {
  2885		switch dt := dt.(type) {
  2886		default:
  2887			fatalf("%s: unexpected type: %s", lineno(pos), dt)
  2888			return false
  2889	
  2890		case *dwarf.AddrType, *dwarf.BoolType, *dwarf.CharType, *dwarf.EnumType,
  2891			*dwarf.FloatType, *dwarf.ComplexType, *dwarf.FuncType,
  2892			*dwarf.IntType, *dwarf.UcharType, *dwarf.UintType, *dwarf.VoidType:
  2893	
  2894			return false
  2895	
  2896		case *dwarf.ArrayType:
  2897			return c.dwarfHasPointer(dt.Type, pos)
  2898	
  2899		case *dwarf.PtrType:
  2900			return true
  2901	
  2902		case *dwarf.QualType:
  2903			return c.dwarfHasPointer(dt.Type, pos)
  2904	
  2905		case *dwarf.StructType:
  2906			for _, f := range dt.Field {
  2907				if c.dwarfHasPointer(f.Type, pos) {
  2908					return true
  2909				}
  2910			}
  2911			return false
  2912	
  2913		case *dwarf.TypedefType:
  2914			if dt.Name == "_GoString_" || dt.Name == "_GoBytes_" {
  2915				return true
  2916			}
  2917			return c.dwarfHasPointer(dt.Type, pos)
  2918		}
  2919	}
  2920	
  2921	func upper(s string) string {
  2922		if s == "" {
  2923			return ""
  2924		}
  2925		r, size := utf8.DecodeRuneInString(s)
  2926		if r == '_' {
  2927			return "X" + s
  2928		}
  2929		return string(unicode.ToUpper(r)) + s[size:]
  2930	}
  2931	
  2932	// godefsFields rewrites field names for use in Go or C definitions.
  2933	// It strips leading common prefixes (like tv_ in tv_sec, tv_usec)
  2934	// converts names to upper case, and rewrites _ into Pad_godefs_n,
  2935	// so that all fields are exported.
  2936	func godefsFields(fld []*ast.Field) {
  2937		prefix := fieldPrefix(fld)
  2938		npad := 0
  2939		for _, f := range fld {
  2940			for _, n := range f.Names {
  2941				if n.Name != prefix {
  2942					n.Name = strings.TrimPrefix(n.Name, prefix)
  2943				}
  2944				if n.Name == "_" {
  2945					// Use exported name instead.
  2946					n.Name = "Pad_cgo_" + strconv.Itoa(npad)
  2947					npad++
  2948				}
  2949				n.Name = upper(n.Name)
  2950			}
  2951		}
  2952	}
  2953	
  2954	// fieldPrefix returns the prefix that should be removed from all the
  2955	// field names when generating the C or Go code. For generated
  2956	// C, we leave the names as is (tv_sec, tv_usec), since that's what
  2957	// people are used to seeing in C.  For generated Go code, such as
  2958	// package syscall's data structures, we drop a common prefix
  2959	// (so sec, usec, which will get turned into Sec, Usec for exporting).
  2960	func fieldPrefix(fld []*ast.Field) string {
  2961		prefix := ""
  2962		for _, f := range fld {
  2963			for _, n := range f.Names {
  2964				// Ignore field names that don't have the prefix we're
  2965				// looking for. It is common in C headers to have fields
  2966				// named, say, _pad in an otherwise prefixed header.
  2967				// If the struct has 3 fields tv_sec, tv_usec, _pad1, then we
  2968				// still want to remove the tv_ prefix.
  2969				// The check for "orig_" here handles orig_eax in the
  2970				// x86 ptrace register sets, which otherwise have all fields
  2971				// with reg_ prefixes.
  2972				if strings.HasPrefix(n.Name, "orig_") || strings.HasPrefix(n.Name, "_") {
  2973					continue
  2974				}
  2975				i := strings.Index(n.Name, "_")
  2976				if i < 0 {
  2977					continue
  2978				}
  2979				if prefix == "" {
  2980					prefix = n.Name[:i+1]
  2981				} else if prefix != n.Name[:i+1] {
  2982					return ""
  2983				}
  2984			}
  2985		}
  2986		return prefix
  2987	}
  2988	
  2989	// badPointerTypedef reports whether t is a C typedef that should not be considered a pointer in Go.
  2990	// A typedef is bad if C code sometimes stores non-pointers in this type.
  2991	// TODO: Currently our best solution is to find these manually and list them as
  2992	// they come up. A better solution is desired.
  2993	func (c *typeConv) badPointerTypedef(dt *dwarf.TypedefType) bool {
  2994		if c.badCFType(dt) {
  2995			return true
  2996		}
  2997		if c.badJNI(dt) {
  2998			return true
  2999		}
  3000		if c.badEGLDisplay(dt) {
  3001			return true
  3002		}
  3003		return false
  3004	}
  3005	
  3006	// baseBadPointerTypedef reports whether the base of a chain of typedefs is a bad typedef
  3007	// as badPointerTypedef reports.
  3008	func (c *typeConv) baseBadPointerTypedef(dt *dwarf.TypedefType) bool {
  3009		for {
  3010			if t, ok := dt.Type.(*dwarf.TypedefType); ok {
  3011				dt = t
  3012				continue
  3013			}
  3014			break
  3015		}
  3016		return c.badPointerTypedef(dt)
  3017	}
  3018	
  3019	func (c *typeConv) badCFType(dt *dwarf.TypedefType) bool {
  3020		// The real bad types are CFNumberRef and CFDateRef.
  3021		// Sometimes non-pointers are stored in these types.
  3022		// CFTypeRef is a supertype of those, so it can have bad pointers in it as well.
  3023		// We return true for the other *Ref types just so casting between them is easier.
  3024		// We identify the correct set of types as those ending in Ref and for which
  3025		// there exists a corresponding GetTypeID function.
  3026		// See comment below for details about the bad pointers.
  3027		if goos != "darwin" {
  3028			return false
  3029		}
  3030		s := dt.Name
  3031		if !strings.HasSuffix(s, "Ref") {
  3032			return false
  3033		}
  3034		s = s[:len(s)-3]
  3035		if s == "CFType" {
  3036			return true
  3037		}
  3038		if c.getTypeIDs[s] {
  3039			return true
  3040		}
  3041		if i := strings.Index(s, "Mutable"); i >= 0 && c.getTypeIDs[s[:i]+s[i+7:]] {
  3042			// Mutable and immutable variants share a type ID.
  3043			return true
  3044		}
  3045		return false
  3046	}
  3047	
  3048	// Comment from Darwin's CFInternal.h
  3049	/*
  3050	// Tagged pointer support
  3051	// Low-bit set means tagged object, next 3 bits (currently)
  3052	// define the tagged object class, next 4 bits are for type
  3053	// information for the specific tagged object class.  Thus,
  3054	// the low byte is for type info, and the rest of a pointer
  3055	// (32 or 64-bit) is for payload, whatever the tagged class.
  3056	//
  3057	// Note that the specific integers used to identify the
  3058	// specific tagged classes can and will change from release
  3059	// to release (that's why this stuff is in CF*Internal*.h),
  3060	// as can the definition of type info vs payload above.
  3061	//
  3062	#if __LP64__
  3063	#define CF_IS_TAGGED_OBJ(PTR)	((uintptr_t)(PTR) & 0x1)
  3064	#define CF_TAGGED_OBJ_TYPE(PTR)	((uintptr_t)(PTR) & 0xF)
  3065	#else
  3066	#define CF_IS_TAGGED_OBJ(PTR)	0
  3067	#define CF_TAGGED_OBJ_TYPE(PTR)	0
  3068	#endif
  3069	
  3070	enum {
  3071	    kCFTaggedObjectID_Invalid = 0,
  3072	    kCFTaggedObjectID_Atom = (0 << 1) + 1,
  3073	    kCFTaggedObjectID_Undefined3 = (1 << 1) + 1,
  3074	    kCFTaggedObjectID_Undefined2 = (2 << 1) + 1,
  3075	    kCFTaggedObjectID_Integer = (3 << 1) + 1,
  3076	    kCFTaggedObjectID_DateTS = (4 << 1) + 1,
  3077	    kCFTaggedObjectID_ManagedObjectID = (5 << 1) + 1, // Core Data
  3078	    kCFTaggedObjectID_Date = (6 << 1) + 1,
  3079	    kCFTaggedObjectID_Undefined7 = (7 << 1) + 1,
  3080	};
  3081	*/
  3082	
  3083	func (c *typeConv) badJNI(dt *dwarf.TypedefType) bool {
  3084		// In Dalvik and ART, the jobject type in the JNI interface of the JVM has the
  3085		// property that it is sometimes (always?) a small integer instead of a real pointer.
  3086		// Note: although only the android JVMs are bad in this respect, we declare the JNI types
  3087		// bad regardless of platform, so the same Go code compiles on both android and non-android.
  3088		if parent, ok := jniTypes[dt.Name]; ok {
  3089			// Try to make sure we're talking about a JNI type, not just some random user's
  3090			// type that happens to use the same name.
  3091			// C doesn't have the notion of a package, so it's hard to be certain.
  3092	
  3093			// Walk up to jobject, checking each typedef on the way.
  3094			w := dt
  3095			for parent != "" {
  3096				t, ok := w.Type.(*dwarf.TypedefType)
  3097				if !ok || t.Name != parent {
  3098					return false
  3099				}
  3100				w = t
  3101				parent, ok = jniTypes[w.Name]
  3102				if !ok {
  3103					return false
  3104				}
  3105			}
  3106	
  3107			// Check that the typedef is either:
  3108			// 1:
  3109			//     	struct _jobject;
  3110			//     	typedef struct _jobject *jobject;
  3111			// 2: (in NDK16 in C++)
  3112			//     	class _jobject {};
  3113			//     	typedef _jobject* jobject;
  3114			// 3: (in NDK16 in C)
  3115			//     	typedef void* jobject;
  3116			if ptr, ok := w.Type.(*dwarf.PtrType); ok {
  3117				switch v := ptr.Type.(type) {
  3118				case *dwarf.VoidType:
  3119					return true
  3120				case *dwarf.StructType:
  3121					if v.StructName == "_jobject" && len(v.Field) == 0 {
  3122						switch v.Kind {
  3123						case "struct":
  3124							if v.Incomplete {
  3125								return true
  3126							}
  3127						case "class":
  3128							if !v.Incomplete {
  3129								return true
  3130							}
  3131						}
  3132					}
  3133				}
  3134			}
  3135		}
  3136		return false
  3137	}
  3138	
  3139	func (c *typeConv) badEGLDisplay(dt *dwarf.TypedefType) bool {
  3140		if dt.Name != "EGLDisplay" {
  3141			return false
  3142		}
  3143		// Check that the typedef is "typedef void *EGLDisplay".
  3144		if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
  3145			if _, ok := ptr.Type.(*dwarf.VoidType); ok {
  3146				return true
  3147			}
  3148		}
  3149		return false
  3150	}
  3151	
  3152	// jniTypes maps from JNI types that we want to be uintptrs, to the underlying type to which
  3153	// they are mapped. The base "jobject" maps to the empty string.
  3154	var jniTypes = map[string]string{
  3155		"jobject":       "",
  3156		"jclass":        "jobject",
  3157		"jthrowable":    "jobject",
  3158		"jstring":       "jobject",
  3159		"jarray":        "jobject",
  3160		"jbooleanArray": "jarray",
  3161		"jbyteArray":    "jarray",
  3162		"jcharArray":    "jarray",
  3163		"jshortArray":   "jarray",
  3164		"jintArray":     "jarray",
  3165		"jlongArray":    "jarray",
  3166		"jfloatArray":   "jarray",
  3167		"jdoubleArray":  "jarray",
  3168		"jobjectArray":  "jarray",
  3169		"jweak":         "jobject",
  3170	}
  3171	

View as plain text