1 // unsafe.cc -- Go frontend builtin unsafe package.
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.
13 // Set up the builtin unsafe package. This should probably be driven
17 Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
18 source_location location)
20 location_t bloc = BUILTINS_LOCATION;
23 Package* package = this->add_imported_package("unsafe", local_name,
24 is_local_name_exported,
26 location, &add_to_globals);
30 go_assert(saw_errors());
34 package->set_is_imported();
36 Bindings* bindings = package->bindings();
38 // The type may have already been created by an import.
39 Named_object* no = package->bindings()->lookup("Pointer");
42 Type* type = Type::make_pointer_type(Type::make_void_type());
43 no = bindings->add_type("Pointer", package, type, UNKNOWN_LOCATION);
47 go_assert(no->package() == package);
48 go_assert(no->is_type());
49 go_assert(no->type_value()->is_unsafe_pointer_type());
50 no->type_value()->set_is_visible();
52 Named_type* pointer_type = no->type_value();
54 this->add_named_type(pointer_type);
56 Type* int_type = this->lookup_global("int")->type_value();
59 Typed_identifier_list* results = new Typed_identifier_list;
60 results->push_back(Typed_identifier("", int_type, bloc));
61 Function_type* fntype = Type::make_function_type(NULL, NULL, results, bloc);
62 fntype->set_is_builtin();
63 no = bindings->add_function_declaration("Sizeof", package, fntype, bloc);
65 this->add_named_object(no);
68 results = new Typed_identifier_list;
69 results->push_back(Typed_identifier("", int_type, bloc));
70 fntype = Type::make_function_type(NULL, NULL, results, bloc);
71 fntype->set_is_varargs();
72 fntype->set_is_builtin();
73 no = bindings->add_function_declaration("Offsetof", package, fntype, bloc);
75 this->add_named_object(no);
78 results = new Typed_identifier_list;
79 results->push_back(Typed_identifier("", int_type, bloc));
80 fntype = Type::make_function_type(NULL, NULL, results, bloc);
81 fntype->set_is_varargs();
82 fntype->set_is_builtin();
83 no = bindings->add_function_declaration("Alignof", package, fntype, bloc);
85 this->add_named_object(no);
88 Type* empty_interface = Type::make_interface_type(NULL, bloc);
89 Typed_identifier_list* parameters = new Typed_identifier_list;
90 parameters->push_back(Typed_identifier("i", empty_interface, bloc));
91 results = new Typed_identifier_list;
92 results->push_back(Typed_identifier("", empty_interface, bloc));
93 fntype = Type::make_function_type(NULL, parameters, results, bloc);
94 no = bindings->add_function_declaration("Typeof", package, fntype, bloc);
96 this->add_named_object(no);
99 parameters = new Typed_identifier_list;
100 parameters->push_back(Typed_identifier("it", empty_interface, bloc));
101 results = new Typed_identifier_list;
102 results->push_back(Typed_identifier("", empty_interface, bloc));
103 results->push_back(Typed_identifier("", pointer_type, bloc));
104 fntype = Type::make_function_type(NULL, parameters, results, bloc);
105 no = bindings->add_function_declaration("Reflect", package, fntype, bloc);
107 this->add_named_object(no);
110 parameters = new Typed_identifier_list;
111 parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
112 parameters->push_back(Typed_identifier("addr", pointer_type, bloc));
113 results = new Typed_identifier_list;
114 results->push_back(Typed_identifier("", empty_interface, bloc));
115 fntype = Type::make_function_type(NULL, parameters, results, bloc);
116 no = bindings->add_function_declaration("Unreflect", package, fntype, bloc);
118 this->add_named_object(no);
121 parameters = new Typed_identifier_list;
122 parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
123 results = new Typed_identifier_list;
124 results->push_back(Typed_identifier("", pointer_type, bloc));
125 fntype = Type::make_function_type(NULL, parameters, results, bloc);
126 no = bindings->add_function_declaration("New", package, fntype, bloc);
128 this->add_named_object(no);
131 parameters = new Typed_identifier_list;
132 parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
133 parameters->push_back(Typed_identifier("n", int_type, bloc));
134 results = new Typed_identifier_list;
135 results->push_back(Typed_identifier("", pointer_type, bloc));
136 fntype = Type::make_function_type(NULL, parameters, results, bloc);
137 no = bindings->add_function_declaration("NewArray", package, fntype, bloc);
139 this->add_named_object(no);
141 if (!this->imported_unsafe_)
143 go_imported_unsafe();
144 this->imported_unsafe_ = true;