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
);
27 package
->set_is_imported();
29 Bindings
* bindings
= package
->bindings();
31 // The type may have already been created by an import.
32 Named_object
* no
= package
->bindings()->lookup("Pointer");
35 Type
* type
= Type::make_pointer_type(Type::make_void_type());
36 no
= bindings
->add_type("Pointer", package
, type
, UNKNOWN_LOCATION
);
40 gcc_assert(no
->package() == package
);
41 gcc_assert(no
->is_type());
42 gcc_assert(no
->type_value()->is_unsafe_pointer_type());
43 no
->type_value()->set_is_visible();
45 Named_type
* pointer_type
= no
->type_value();
47 this->add_named_type(pointer_type
);
49 Type
* int_type
= this->lookup_global("int")->type_value();
52 Typed_identifier_list
* results
= new Typed_identifier_list
;
53 results
->push_back(Typed_identifier("", int_type
, bloc
));
54 Function_type
* fntype
= Type::make_function_type(NULL
, NULL
, results
, bloc
);
55 fntype
->set_is_builtin();
56 no
= bindings
->add_function_declaration("Sizeof", package
, fntype
, bloc
);
58 this->add_named_object(no
);
61 results
= new Typed_identifier_list
;
62 results
->push_back(Typed_identifier("", int_type
, bloc
));
63 fntype
= Type::make_function_type(NULL
, NULL
, results
, bloc
);
64 fntype
->set_is_varargs();
65 fntype
->set_is_builtin();
66 no
= bindings
->add_function_declaration("Offsetof", package
, fntype
, bloc
);
68 this->add_named_object(no
);
71 results
= new Typed_identifier_list
;
72 results
->push_back(Typed_identifier("", int_type
, bloc
));
73 fntype
= Type::make_function_type(NULL
, NULL
, results
, bloc
);
74 fntype
->set_is_varargs();
75 fntype
->set_is_builtin();
76 no
= bindings
->add_function_declaration("Alignof", package
, fntype
, bloc
);
78 this->add_named_object(no
);
81 Type
* empty_interface
= Type::make_interface_type(NULL
, bloc
);
82 Typed_identifier_list
* parameters
= new Typed_identifier_list
;
83 parameters
->push_back(Typed_identifier("i", empty_interface
, bloc
));
84 results
= new Typed_identifier_list
;
85 results
->push_back(Typed_identifier("", empty_interface
, bloc
));
86 fntype
= Type::make_function_type(NULL
, parameters
, results
, bloc
);
87 no
= bindings
->add_function_declaration("Typeof", package
, fntype
, bloc
);
89 this->add_named_object(no
);
92 parameters
= new Typed_identifier_list
;
93 parameters
->push_back(Typed_identifier("it", empty_interface
, bloc
));
94 results
= new Typed_identifier_list
;
95 results
->push_back(Typed_identifier("", empty_interface
, bloc
));
96 results
->push_back(Typed_identifier("", pointer_type
, bloc
));
97 fntype
= Type::make_function_type(NULL
, parameters
, results
, bloc
);
98 no
= bindings
->add_function_declaration("Reflect", package
, fntype
, bloc
);
100 this->add_named_object(no
);
103 parameters
= new Typed_identifier_list
;
104 parameters
->push_back(Typed_identifier("typ", empty_interface
, bloc
));
105 parameters
->push_back(Typed_identifier("addr", pointer_type
, bloc
));
106 results
= new Typed_identifier_list
;
107 results
->push_back(Typed_identifier("", empty_interface
, bloc
));
108 fntype
= Type::make_function_type(NULL
, parameters
, results
, bloc
);
109 no
= bindings
->add_function_declaration("Unreflect", package
, fntype
, bloc
);
111 this->add_named_object(no
);
114 parameters
= new Typed_identifier_list
;
115 parameters
->push_back(Typed_identifier("typ", empty_interface
, bloc
));
116 results
= new Typed_identifier_list
;
117 results
->push_back(Typed_identifier("", pointer_type
, bloc
));
118 fntype
= Type::make_function_type(NULL
, parameters
, results
, bloc
);
119 no
= bindings
->add_function_declaration("New", package
, fntype
, bloc
);
121 this->add_named_object(no
);
124 parameters
= new Typed_identifier_list
;
125 parameters
->push_back(Typed_identifier("typ", empty_interface
, bloc
));
126 parameters
->push_back(Typed_identifier("n", int_type
, bloc
));
127 results
= new Typed_identifier_list
;
128 results
->push_back(Typed_identifier("", pointer_type
, bloc
));
129 fntype
= Type::make_function_type(NULL
, parameters
, results
, bloc
);
130 no
= bindings
->add_function_declaration("NewArray", package
, fntype
, bloc
);
132 this->add_named_object(no
);
134 if (!this->imported_unsafe_
)
136 go_imported_unsafe();
137 this->imported_unsafe_
= true;