blob: 80b367cc46d4b6fc03cfc44236e00a99a642bf8d [file] [log] [blame]
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -08001// 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
Ian Lance Taylor5e2a3c12011-01-13 16:54:22 -08009#include "go-c.h"
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080010#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
16void
17Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
18 source_location location)
19{
20 location_t bloc = BUILTINS_LOCATION;
21
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080022 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);
Ian Lance Taylorcf5fa622011-02-21 19:23:12 -080027
28 if (package == NULL)
29 {
Evan Shawcafc2f72011-04-21 15:51:28 -070030 go_assert(saw_errors());
Ian Lance Taylorcf5fa622011-02-21 19:23:12 -080031 return;
32 }
33
Ian Lance Taylor41187c92010-01-30 23:50:36 -080034 package->set_is_imported();
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080035
36 Bindings* bindings = package->bindings();
37
38 // The type may have already been created by an import.
39 Named_object* no = package->bindings()->lookup("Pointer");
40 if (no == NULL)
41 {
42 Type* type = Type::make_pointer_type(Type::make_void_type());
43 no = bindings->add_type("Pointer", package, type, UNKNOWN_LOCATION);
44 }
45 else
46 {
Evan Shawcafc2f72011-04-21 15:51:28 -070047 go_assert(no->package() == package);
48 go_assert(no->is_type());
49 go_assert(no->type_value()->is_unsafe_pointer_type());
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080050 no->type_value()->set_is_visible();
51 }
52 Named_type* pointer_type = no->type_value();
Ian Lance Taylor6d27f402010-01-30 22:35:06 -080053 if (add_to_globals)
54 this->add_named_type(pointer_type);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080055
56 Type* int_type = this->lookup_global("int")->type_value();
57
58 // Sizeof.
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();
Ian Lance Taylor6d27f402010-01-30 22:35:06 -080063 no = bindings->add_function_declaration("Sizeof", package, fntype, bloc);
64 if (add_to_globals)
65 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080066
67 // Offsetof.
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();
Ian Lance Taylor6d27f402010-01-30 22:35:06 -080073 no = bindings->add_function_declaration("Offsetof", package, fntype, bloc);
74 if (add_to_globals)
75 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080076
77 // Alignof.
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);
Ian Lance Taylor6d27f402010-01-30 22:35:06 -080084 if (add_to_globals)
85 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080086
87 // Typeof.
Ian Lance Taylor2492bac2010-02-05 18:32:05 -080088 Type* empty_interface = Type::make_interface_type(NULL, bloc);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080089 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);
Ian Lance Taylor6d27f402010-01-30 22:35:06 -080094 no = bindings->add_function_declaration("Typeof", package, fntype, bloc);
95 if (add_to_globals)
96 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080097
98 // Reflect.
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);
Ian Lance Taylor6d27f402010-01-30 22:35:06 -0800105 no = bindings->add_function_declaration("Reflect", package, fntype, bloc);
106 if (add_to_globals)
107 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800108
109 // Unreflect.
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);
Ian Lance Taylor6d27f402010-01-30 22:35:06 -0800116 no = bindings->add_function_declaration("Unreflect", package, fntype, bloc);
117 if (add_to_globals)
118 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800119
120 // New.
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);
Ian Lance Taylor6d27f402010-01-30 22:35:06 -0800126 no = bindings->add_function_declaration("New", package, fntype, bloc);
127 if (add_to_globals)
128 this->add_named_object(no);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800129
130 // NewArray.
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);
Ian Lance Taylor6d27f402010-01-30 22:35:06 -0800137 no = bindings->add_function_declaration("NewArray", package, fntype, bloc);
138 if (add_to_globals)
139 this->add_named_object(no);
Ian Lance Taylor29caa552010-07-23 09:44:07 -0700140
Ian Lance Taylor5e2a3c12011-01-13 16:54:22 -0800141 if (!this->imported_unsafe_)
142 {
143 go_imported_unsafe();
144 this->imported_unsafe_ = true;
145 }
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800146}