blob: c4a9346ca3e331c22a657c156f2246bfbd416b9c [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
Ian Lance Taylord5d51242021-08-06 12:01:04 -070013// Set up the builtin unsafe package.
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080014
15void
16Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
Ian Lance Taylor4b5e8ee2011-11-29 11:02:01 -080017 Location location)
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080018{
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080019 bool add_to_globals;
20 Package* package = this->add_imported_package("unsafe", local_name,
21 is_local_name_exported,
Ian Lance Taylorc0295932015-01-29 16:35:18 -080022 "unsafe", "unsafe", location,
Ian Lance Taylor3884fcf2012-05-14 14:57:59 -070023 &add_to_globals);
Ian Lance Taylorcf5fa622011-02-21 19:23:12 -080024
25 if (package == NULL)
26 {
Evan Shawcafc2f72011-04-21 15:51:28 -070027 go_assert(saw_errors());
Ian Lance Taylorcf5fa622011-02-21 19:23:12 -080028 return;
29 }
30
Ian Lance Taylor85bf82e2011-09-26 12:34:51 -070031 package->set_location(location);
Ian Lance Taylor5f856ba2012-02-17 15:19:05 -080032 this->imports_.insert(std::make_pair("unsafe", package));
33
Ian Lance Taylord5d51242021-08-06 12:01:04 -070034 this->add_unsafe_bindings(package);
35
36 Named_object* pointer_no = package->bindings()->lookup_local("Pointer");
37 pointer_no->type_value()->set_is_visible();
38
39 if (add_to_globals)
40 {
41 Bindings* bindings = package->bindings();
42 for (Bindings::const_declarations_iterator p =
43 bindings->begin_declarations();
44 p != bindings->end_declarations();
45 ++p)
46 this->add_dot_import_object(p->second);
47 }
48}
49
50// Add the unsafe bindings to the Package object. This should
51// probably be driven by a table.
52
53void
54Gogo::add_unsafe_bindings(Package* package)
55{
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080056 Bindings* bindings = package->bindings();
57
Ian Lance Taylord5d51242021-08-06 12:01:04 -070058 if (bindings->lookup_local("Sizeof") != NULL)
59 {
60 // Already done by an earlier import.
61 return;
62 }
63
64 Location bloc = Linemap::predeclared_location();
65
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080066 // The type may have already been created by an import.
Ian Lance Taylord5d51242021-08-06 12:01:04 -070067 Named_object* no = bindings->lookup("Pointer");
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080068 if (no == NULL)
69 {
70 Type* type = Type::make_pointer_type(Type::make_void_type());
Ian Lance Taylor4b5e8ee2011-11-29 11:02:01 -080071 no = bindings->add_type("Pointer", package, type,
72 Linemap::unknown_location());
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080073 }
74 else
75 {
Evan Shawcafc2f72011-04-21 15:51:28 -070076 go_assert(no->package() == package);
77 go_assert(no->is_type());
78 go_assert(no->type_value()->is_unsafe_pointer_type());
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080079 }
80 Named_type* pointer_type = no->type_value();
Ian Lance Taylord5d51242021-08-06 12:01:04 -070081
82 // This may be called during an import, so the type may not be
83 // visible yet.
84 pointer_type->clear_is_visible();
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080085
Ian Lance Taylorf4e5dcb2012-03-28 20:51:50 -070086 Type* uintptr_type = Type::lookup_integer_type("uintptr");
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080087
88 // Sizeof.
89 Typed_identifier_list* results = new Typed_identifier_list;
Ian Lance Taylorf4e5dcb2012-03-28 20:51:50 -070090 results->push_back(Typed_identifier("", uintptr_type, bloc));
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080091 Function_type* fntype = Type::make_function_type(NULL, NULL, results, bloc);
92 fntype->set_is_builtin();
Ian Lance Taylord5d51242021-08-06 12:01:04 -070093 bindings->add_function_declaration("Sizeof", package, fntype, bloc);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080094
95 // Offsetof.
96 results = new Typed_identifier_list;
Ian Lance Taylorf4e5dcb2012-03-28 20:51:50 -070097 results->push_back(Typed_identifier("", uintptr_type, bloc));
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -080098 fntype = Type::make_function_type(NULL, NULL, results, bloc);
99 fntype->set_is_varargs();
100 fntype->set_is_builtin();
Ian Lance Taylord5d51242021-08-06 12:01:04 -0700101 bindings->add_function_declaration("Offsetof", package, fntype, bloc);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800102
103 // Alignof.
104 results = new Typed_identifier_list;
Ian Lance Taylorf4e5dcb2012-03-28 20:51:50 -0700105 results->push_back(Typed_identifier("", uintptr_type, bloc));
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800106 fntype = Type::make_function_type(NULL, NULL, results, bloc);
107 fntype->set_is_varargs();
108 fntype->set_is_builtin();
Ian Lance Taylord5d51242021-08-06 12:01:04 -0700109 bindings->add_function_declaration("Alignof", package, fntype, bloc);
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800110
Ian Lance Taylorad667e72021-07-31 19:28:51 -0700111 // Add.
112 results = new Typed_identifier_list;
113 results->push_back(Typed_identifier("", pointer_type, bloc));
114 fntype = Type::make_function_type(NULL, NULL, results, bloc);
115 fntype->set_is_builtin();
Ian Lance Taylord5d51242021-08-06 12:01:04 -0700116 bindings->add_function_declaration("Add", package, fntype, bloc);
Ian Lance Taylorad667e72021-07-31 19:28:51 -0700117
118 // Slice.
119 fntype = Type::make_function_type(NULL, NULL, NULL, bloc);
120 fntype->set_is_builtin();
Ian Lance Taylord5d51242021-08-06 12:01:04 -0700121 bindings->add_function_declaration("Slice", package, fntype, bloc);
Ian Lance Taylorad667e72021-07-31 19:28:51 -0700122
Ian Lance Taylor5e2a3c12011-01-13 16:54:22 -0800123 if (!this->imported_unsafe_)
124 {
125 go_imported_unsafe();
126 this->imported_unsafe_ = true;
127 }
Ian Lance Taylor0ef89c42010-01-29 13:33:36 -0800128}