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,
20 Location bloc = Linemap::predeclared_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_location(location);
35 package->set_is_imported();
37 Bindings* bindings = package->bindings();
39 // The type may have already been created by an import.
40 Named_object* no = package->bindings()->lookup("Pointer");
43 Type* type = Type::make_pointer_type(Type::make_void_type());
44 no = bindings->add_type("Pointer", package, type,
45 Linemap::unknown_location());
49 go_assert(no->package() == package);
50 go_assert(no->is_type());
51 go_assert(no->type_value()->is_unsafe_pointer_type());
52 no->type_value()->set_is_visible();
54 Named_type* pointer_type = no->type_value();
56 this->add_named_type(pointer_type);
58 Type* int_type = this->lookup_global("int")->type_value();
61 Typed_identifier_list* results = new Typed_identifier_list;
62 results->push_back(Typed_identifier("", int_type, bloc));
63 Function_type* fntype = Type::make_function_type(NULL, NULL, results, bloc);
64 fntype->set_is_builtin();
65 no = bindings->add_function_declaration("Sizeof", package, fntype, bloc);
67 this->add_named_object(no);
70 results = new Typed_identifier_list;
71 results->push_back(Typed_identifier("", int_type, bloc));
72 fntype = Type::make_function_type(NULL, NULL, results, bloc);
73 fntype->set_is_varargs();
74 fntype->set_is_builtin();
75 no = bindings->add_function_declaration("Offsetof", package, fntype, bloc);
77 this->add_named_object(no);
80 results = new Typed_identifier_list;
81 results->push_back(Typed_identifier("", int_type, bloc));
82 fntype = Type::make_function_type(NULL, NULL, results, bloc);
83 fntype->set_is_varargs();
84 fntype->set_is_builtin();
85 no = bindings->add_function_declaration("Alignof", package, fntype, bloc);
87 this->add_named_object(no);
90 Type* empty_interface = Type::make_interface_type(NULL, bloc);
91 Typed_identifier_list* parameters = new Typed_identifier_list;
92 parameters->push_back(Typed_identifier("i", empty_interface, bloc));
93 results = new Typed_identifier_list;
94 results->push_back(Typed_identifier("", empty_interface, bloc));
95 fntype = Type::make_function_type(NULL, parameters, results, bloc);
96 no = bindings->add_function_declaration("Typeof", package, fntype, bloc);
98 this->add_named_object(no);
101 parameters = new Typed_identifier_list;
102 parameters->push_back(Typed_identifier("it", empty_interface, bloc));
103 results = new Typed_identifier_list;
104 results->push_back(Typed_identifier("", empty_interface, bloc));
105 results->push_back(Typed_identifier("", pointer_type, bloc));
106 fntype = Type::make_function_type(NULL, parameters, results, bloc);
107 no = bindings->add_function_declaration("Reflect", package, fntype, bloc);
109 this->add_named_object(no);
112 parameters = new Typed_identifier_list;
113 parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
114 parameters->push_back(Typed_identifier("addr", pointer_type, bloc));
115 results = new Typed_identifier_list;
116 results->push_back(Typed_identifier("", empty_interface, bloc));
117 fntype = Type::make_function_type(NULL, parameters, results, bloc);
118 no = bindings->add_function_declaration("Unreflect", package, fntype, bloc);
120 this->add_named_object(no);
123 parameters = new Typed_identifier_list;
124 parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
125 results = new Typed_identifier_list;
126 results->push_back(Typed_identifier("", pointer_type, bloc));
127 fntype = Type::make_function_type(NULL, parameters, results, bloc);
128 no = bindings->add_function_declaration("New", package, fntype, bloc);
130 this->add_named_object(no);
133 parameters = new Typed_identifier_list;
134 parameters->push_back(Typed_identifier("typ", empty_interface, bloc));
135 parameters->push_back(Typed_identifier("n", int_type, bloc));
136 results = new Typed_identifier_list;
137 results->push_back(Typed_identifier("", pointer_type, bloc));
138 fntype = Type::make_function_type(NULL, parameters, results, bloc);
139 no = bindings->add_function_declaration("NewArray", package, fntype, bloc);
141 this->add_named_object(no);
143 if (!this->imported_unsafe_)
145 go_imported_unsafe();
146 this->imported_unsafe_ = true;