Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 1 | // 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 Taylor | 5e2a3c1 | 2011-01-13 16:54:22 -0800 | [diff] [blame] | 9 | #include "go-c.h" |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 10 | #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 | |
| 16 | void |
| 17 | Gogo::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 Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 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); |
Ian Lance Taylor | cf5fa62 | 2011-02-21 19:23:12 -0800 | [diff] [blame] | 27 | |
| 28 | if (package == NULL) |
| 29 | { |
Evan Shaw | cafc2f7 | 2011-04-21 15:51:28 -0700 | [diff] [blame^] | 30 | go_assert(saw_errors()); |
Ian Lance Taylor | cf5fa62 | 2011-02-21 19:23:12 -0800 | [diff] [blame] | 31 | return; |
| 32 | } |
| 33 | |
Ian Lance Taylor | 41187c9 | 2010-01-30 23:50:36 -0800 | [diff] [blame] | 34 | package->set_is_imported(); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 35 | |
| 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 Shaw | cafc2f7 | 2011-04-21 15:51:28 -0700 | [diff] [blame^] | 47 | go_assert(no->package() == package); |
| 48 | go_assert(no->is_type()); |
| 49 | go_assert(no->type_value()->is_unsafe_pointer_type()); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 50 | no->type_value()->set_is_visible(); |
| 51 | } |
| 52 | Named_type* pointer_type = no->type_value(); |
Ian Lance Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 53 | if (add_to_globals) |
| 54 | this->add_named_type(pointer_type); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 55 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 63 | no = bindings->add_function_declaration("Sizeof", package, fntype, bloc); |
| 64 | if (add_to_globals) |
| 65 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 66 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 73 | no = bindings->add_function_declaration("Offsetof", package, fntype, bloc); |
| 74 | if (add_to_globals) |
| 75 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 76 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 84 | if (add_to_globals) |
| 85 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 86 | |
| 87 | // Typeof. |
Ian Lance Taylor | 2492bac | 2010-02-05 18:32:05 -0800 | [diff] [blame] | 88 | Type* empty_interface = Type::make_interface_type(NULL, bloc); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 89 | 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 94 | no = bindings->add_function_declaration("Typeof", package, fntype, bloc); |
| 95 | if (add_to_globals) |
| 96 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 97 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 105 | no = bindings->add_function_declaration("Reflect", package, fntype, bloc); |
| 106 | if (add_to_globals) |
| 107 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 108 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 116 | no = bindings->add_function_declaration("Unreflect", package, fntype, bloc); |
| 117 | if (add_to_globals) |
| 118 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 119 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 126 | no = bindings->add_function_declaration("New", package, fntype, bloc); |
| 127 | if (add_to_globals) |
| 128 | this->add_named_object(no); |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 129 | |
| 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 Taylor | 6d27f40 | 2010-01-30 22:35:06 -0800 | [diff] [blame] | 137 | no = bindings->add_function_declaration("NewArray", package, fntype, bloc); |
| 138 | if (add_to_globals) |
| 139 | this->add_named_object(no); |
Ian Lance Taylor | 29caa55 | 2010-07-23 09:44:07 -0700 | [diff] [blame] | 140 | |
Ian Lance Taylor | 5e2a3c1 | 2011-01-13 16:54:22 -0800 | [diff] [blame] | 141 | if (!this->imported_unsafe_) |
| 142 | { |
| 143 | go_imported_unsafe(); |
| 144 | this->imported_unsafe_ = true; |
| 145 | } |
Ian Lance Taylor | 0ef89c4 | 2010-01-29 13:33:36 -0800 | [diff] [blame] | 146 | } |