The type of a string slice is the type of the string being sliced.
[official-gcc.git] / gcc / go / gofrontend / unsafe.cc
blobe219f61edae79427c713ead47574ee24f53fe4f2
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.
7 #include "go-system.h"
9 #include "go-c.h"
10 #include "types.h"
11 #include "gogo.h"
13 // Set up the builtin unsafe package. This should probably be driven
14 // by a table.
16 void
17 Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
18 source_location location)
20 location_t bloc = BUILTINS_LOCATION;
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 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");
33 if (no == NULL)
35 Type* type = Type::make_pointer_type(Type::make_void_type());
36 no = bindings->add_type("Pointer", package, type, UNKNOWN_LOCATION);
38 else
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();
46 if (add_to_globals)
47 this->add_named_type(pointer_type);
49 Type* int_type = this->lookup_global("int")->type_value();
51 // Sizeof.
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);
57 if (add_to_globals)
58 this->add_named_object(no);
60 // Offsetof.
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);
67 if (add_to_globals)
68 this->add_named_object(no);
70 // Alignof.
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);
77 if (add_to_globals)
78 this->add_named_object(no);
80 // Typeof.
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);
88 if (add_to_globals)
89 this->add_named_object(no);
91 // Reflect.
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);
99 if (add_to_globals)
100 this->add_named_object(no);
102 // Unreflect.
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);
110 if (add_to_globals)
111 this->add_named_object(no);
113 // New.
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);
120 if (add_to_globals)
121 this->add_named_object(no);
123 // NewArray.
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);
131 if (add_to_globals)
132 this->add_named_object(no);
134 if (!this->imported_unsafe_)
136 go_imported_unsafe();
137 this->imported_unsafe_ = true;