LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "debug/elf" | 9 "debug/elf" |
10 "debug/macho" | 10 "debug/macho" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // OS X requires its own prefix for a relative path | 28 // OS X requires its own prefix for a relative path |
29 soprefix = "@rpath/" | 29 soprefix = "@rpath/" |
30 } | 30 } |
31 | 31 |
32 fgo2 := creat("_cgo_gotypes.go") | 32 fgo2 := creat("_cgo_gotypes.go") |
33 fc := creat("_cgo_defun.c") | 33 fc := creat("_cgo_defun.c") |
34 fm := creat("_cgo_main.c") | 34 fm := creat("_cgo_main.c") |
35 | 35 |
36 // Write C main file for using gcc to resolve imports. | 36 // Write C main file for using gcc to resolve imports. |
37 fmt.Fprintf(fm, "int main() { return 0; }\n") | 37 fmt.Fprintf(fm, "int main() { return 0; }\n") |
38 » fmt.Fprintf(fm, "int crosscall2;\n\n") | 38 » fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*, int), void *a, int c)
{ }\n") |
| 39 » fmt.Fprintf(fm, "void _cgo_allocate(void *a, int c) { }\n") |
| 40 » fmt.Fprintf(fm, "void _cgo_panic(void *a, int c) { }\n") |
39 | 41 |
40 // Write second Go output: definitions of _C_xxx. | 42 // Write second Go output: definitions of _C_xxx. |
41 // In a separate file so that the import of "unsafe" does not | 43 // In a separate file so that the import of "unsafe" does not |
42 // pollute the original file. | 44 // pollute the original file. |
43 fmt.Fprintf(fgo2, "// Created by cgo - DO NOT EDIT\n\n") | 45 fmt.Fprintf(fgo2, "// Created by cgo - DO NOT EDIT\n\n") |
44 fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName) | 46 fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName) |
45 fmt.Fprintf(fgo2, "import \"unsafe\"\n\n") | 47 fmt.Fprintf(fgo2, "import \"unsafe\"\n\n") |
46 fmt.Fprintf(fgo2, "import \"os\"\n\n") | 48 fmt.Fprintf(fgo2, "import \"os\"\n\n") |
47 fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n") | 49 fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n") |
48 fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n") | 50 fmt.Fprintf(fgo2, "type _ unsafe.Pointer\n\n") |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 off += pad | 178 off += pad |
177 } | 179 } |
178 if n.AddError { | 180 if n.AddError { |
179 fmt.Fprint(&buf, "\t\tvoid *e[2]; /* os.Error */\n") | 181 fmt.Fprint(&buf, "\t\tvoid *e[2]; /* os.Error */\n") |
180 off += 2 * p.PtrSize | 182 off += 2 * p.PtrSize |
181 } | 183 } |
182 if off == 0 { | 184 if off == 0 { |
183 fmt.Fprintf(&buf, "\t\tchar unused;\n") // avoid empty struct | 185 fmt.Fprintf(&buf, "\t\tchar unused;\n") // avoid empty struct |
184 off++ | 186 off++ |
185 } | 187 } |
186 » fmt.Fprintf(&buf, "\t}\n") | 188 » fmt.Fprintf(&buf, "\t}") |
187 return buf.String(), off | 189 return buf.String(), off |
188 } | 190 } |
189 | 191 |
190 func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name, soprefix, sopath str
ing) { | 192 func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name, soprefix, sopath str
ing) { |
191 name := n.Go | 193 name := n.Go |
192 gtype := n.FuncType.Go | 194 gtype := n.FuncType.Go |
193 if n.AddError { | 195 if n.AddError { |
194 // Add "os.Error" to return type list. | 196 // Add "os.Error" to return type list. |
195 // Type list is known to be 0 or 1 element - it's a C function. | 197 // Type list is known to be 0 or 1 element - it's a C function. |
196 err := &ast.Field{Type: ast.NewIdent("os.Error")} | 198 err := &ast.Field{Type: ast.NewIdent("os.Error")} |
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 } | 604 } |
603 if t.Name == "string" { | 605 if t.Name == "string" { |
604 return &Type{Size: p.PtrSize + 4, Align: p.PtrSize, C: "
GoString"} | 606 return &Type{Size: p.PtrSize + 4, Align: p.PtrSize, C: "
GoString"} |
605 } | 607 } |
606 if r, ok := goTypes[t.Name]; ok { | 608 if r, ok := goTypes[t.Name]; ok { |
607 if r.Align > p.PtrSize { | 609 if r.Align > p.PtrSize { |
608 r.Align = p.PtrSize | 610 r.Align = p.PtrSize |
609 } | 611 } |
610 return r | 612 return r |
611 } | 613 } |
| 614 case *ast.SelectorExpr: |
| 615 id, ok := t.X.(*ast.Ident) |
| 616 if ok && id.Name == "unsafe" && t.Sel.Name == "Pointer" { |
| 617 return &Type{Size: p.PtrSize, Align: p.PtrSize, C: "void
*"} |
| 618 } |
612 } | 619 } |
613 error(e.Pos(), "unrecognized Go type %T", e) | 620 error(e.Pos(), "unrecognized Go type %T", e) |
614 return &Type{Size: 4, Align: 4, C: "int"} | 621 return &Type{Size: 4, Align: 4, C: "int"} |
615 } | 622 } |
616 | 623 |
617 const gccProlog = ` | 624 const gccProlog = ` |
618 // Usual nonsense: if x and y are not equal, the type will be invalid | 625 // Usual nonsense: if x and y are not equal, the type will be invalid |
619 // (have a negative array count) and an inscrutable error will come | 626 // (have a negative array count) and an inscrutable error will come |
620 // out of the compiler and hopefully mention "name". | 627 // out of the compiler and hopefully mention "name". |
621 #define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1]; | 628 #define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1]; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 typedef unsigned short ushort; | 686 typedef unsigned short ushort; |
680 typedef long long int64; | 687 typedef long long int64; |
681 typedef unsigned long long uint64; | 688 typedef unsigned long long uint64; |
682 typedef __SIZE_TYPE__ uintptr; | 689 typedef __SIZE_TYPE__ uintptr; |
683 | 690 |
684 typedef struct { char *p; int n; } GoString; | 691 typedef struct { char *p; int n; } GoString; |
685 typedef void *GoMap; | 692 typedef void *GoMap; |
686 typedef void *GoChan; | 693 typedef void *GoChan; |
687 typedef struct { void *t; void *v; } GoInterface; | 694 typedef struct { void *t; void *v; } GoInterface; |
688 ` | 695 ` |
LEFT | RIGHT |