OSDN Git Service

Fix location of error about unused import "unsafe".
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / unsafe.cc
1 // unsafe.cc -- Go frontend builtin unsafe package.
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #include "go-system.h"
8
9 #include "go-c.h"
10 #include "types.h"
11 #include "gogo.h"
12
13 // Set up the builtin unsafe package.  This should probably be driven
14 // by a table.
15
16 void
17 Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
18                     source_location location)
19 {
20   location_t bloc = BUILTINS_LOCATION;
21
22   bool add_to_globals;
23   Package* package = this->add_imported_package("unsafe", local_name,
24                                                 is_local_name_exported,
25                                                 "libgo_unsafe",
26                                                 location, &add_to_globals);
27
28   if (package == NULL)
29     {
30       go_assert(saw_errors());
31       return;
32     }
33
34   package->set_location(location);
35   package->set_is_imported();
36
37   Bindings* bindings = package->bindings();
38
39   // The type may have already been created by an import.
40   Named_object* no = package->bindings()->lookup("Pointer");
41   if (no == NULL)
42     {
43       Type* type = Type::make_pointer_type(Type::make_void_type());
44       no = bindings->add_type("Pointer", package, type, UNKNOWN_LOCATION);
45     }
46   else
47     {
48       go_assert(no->package() == package);
49       go_assert(no->is_type());
50       go_assert(no->type_value()->is_unsafe_pointer_type());
51       no->type_value()->set_is_visible();
52     }
53   Named_type* pointer_type = no->type_value();
54   if (add_to_globals)
55     this->add_named_type(pointer_type);
56
57   Type* int_type = this->lookup_global("int")->type_value();
58
59   // Sizeof.
60   Typed_identifier_list* results = new Typed_identifier_list;
61   results->push_back(Typed_identifier("", int_type, bloc));
62   Function_type* fntype = Type::make_function_type(NULL, NULL, results, bloc);
63   fntype->set_is_builtin();
64   no = bindings->add_function_declaration("Sizeof", package, fntype, bloc);
65   if (add_to_globals)
66     this->add_named_object(no);
67
68   // Offsetof.
69   results = new Typed_identifier_list;
70   results->push_back(Typed_identifier("", int_type, bloc));
71   fntype = Type::make_function_type(NULL, NULL, results, bloc);
72   fntype->set_is_varargs();
73   fntype->set_is_builtin();
74   no = bindings->add_function_declaration("Offsetof", package, fntype, bloc);
75   if (add_to_globals)
76     this->add_named_object(no);
77
78   // Alignof.
79   results = new Typed_identifier_list;
80   results->push_back(Typed_identifier("", int_type, bloc));
81   fntype = Type::make_function_type(NULL, NULL, results, bloc);
82   fntype->set_is_varargs();
83   fntype->set_is_builtin();
84   no = bindings->add_function_declaration("Alignof", package, fntype, bloc);
85   if (add_to_globals)
86     this->add_named_object(no);
87
88   // Typeof.
89   Type* empty_interface = Type::make_interface_type(NULL, bloc);
90   Typed_identifier_list* parameters = new Typed_identifier_list;
91   parameters->push_back(Typed_identifier("i", empty_interface, bloc));
92   results = new Typed_identifier_list;
93   results->push_back(Typed_identifier("", empty_interface, bloc));
94   fntype = Type::make_function_type(NULL, parameters, results, bloc);
95   no = bindings->add_function_declaration("Typeof", package, fntype, bloc);
96   if (add_to_globals)
97     this->add_named_object(no);
98
99   // Reflect.
100   parameters = new Typed_identifier_list;
101   parameters->push_back(Typed_identifier("it", empty_interface, bloc));
102   results = new Typed_identifier_list;
103   results->push_back(Typed_identifier("", empty_interface, bloc));
104   results->push_back(Typed_identifier("", pointer_type, bloc));
105   fntype = Type::make_function_type(NULL, parameters, results, bloc);
106   no = bindings->add_function_declaration("Reflect", package, fntype, bloc);
107   if (add_to_globals)
108     this->add_named_object(no);
109
110   // Unreflect.
111   parameters = new Typed_identifier_list;
112   parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
113   parameters->push_back(Typed_identifier("addr", pointer_type, bloc));
114   results = new Typed_identifier_list;
115   results->push_back(Typed_identifier("", empty_interface, bloc));
116   fntype = Type::make_function_type(NULL, parameters, results, bloc);
117   no = bindings->add_function_declaration("Unreflect", package, fntype, bloc);
118   if (add_to_globals)
119     this->add_named_object(no);
120
121   // New.
122   parameters = new Typed_identifier_list;
123   parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
124   results = new Typed_identifier_list;
125   results->push_back(Typed_identifier("", pointer_type, bloc));
126   fntype = Type::make_function_type(NULL, parameters, results, bloc);
127   no = bindings->add_function_declaration("New", package, fntype, bloc);
128   if (add_to_globals)
129     this->add_named_object(no);
130
131   // NewArray.
132   parameters = new Typed_identifier_list;
133   parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
134   parameters->push_back(Typed_identifier("n", int_type, bloc));
135   results = new Typed_identifier_list;
136   results->push_back(Typed_identifier("", pointer_type, bloc));
137   fntype = Type::make_function_type(NULL, parameters, results, bloc);
138   no = bindings->add_function_declaration("NewArray", package, fntype, bloc);
139   if (add_to_globals)
140     this->add_named_object(no);
141
142   if (!this->imported_unsafe_)
143     {
144       go_imported_unsafe();
145       this->imported_unsafe_ = true;
146     }
147 }