]>
Commit | Line | Data |
---|---|---|
b4c522fa | 1 | /* imports.cc -- Build imported modules/declarations. |
7adcbafe | 2 | Copyright (C) 2014-2022 Free Software Foundation, Inc. |
b4c522fa IB |
3 | |
4 | GCC is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 3, or (at your option) | |
7 | any later version. | |
8 | ||
9 | GCC is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with GCC; see the file COPYING3. If not see | |
16 | <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #include "config.h" | |
19 | #include "system.h" | |
20 | #include "coretypes.h" | |
21 | ||
22 | #include "dmd/aggregate.h" | |
23 | #include "dmd/declaration.h" | |
24 | #include "dmd/enum.h" | |
25 | #include "dmd/identifier.h" | |
26 | #include "dmd/import.h" | |
27 | #include "dmd/module.h" | |
28 | ||
29 | #include "tree.h" | |
30 | #include "stringpool.h" | |
31 | ||
32 | #include "d-tree.h" | |
33 | ||
42d9ff3a | 34 | static hash_map<Dsymbol *, tree> *imported_decls; |
b4c522fa IB |
35 | |
36 | /* Implements the visitor interface to build debug trees for all | |
42d9ff3a IB |
37 | module and import declarations, where RESULT_ holds the back-end |
38 | representation to be cached and returned from the caller. */ | |
b4c522fa IB |
39 | class ImportVisitor : public Visitor |
40 | { | |
41 | using Visitor::visit; | |
42 | ||
42d9ff3a IB |
43 | tree result_; |
44 | ||
b4c522fa IB |
45 | /* Build the declaration DECL as an imported symbol. */ |
46 | tree make_import (tree decl) | |
47 | { | |
48 | gcc_assert (decl != NULL_TREE); | |
49 | ||
50 | tree import = build_decl (input_location, IMPORTED_DECL, | |
51 | DECL_NAME (decl), void_type_node); | |
52 | IMPORTED_DECL_ASSOCIATED_DECL (import) = decl; | |
53 | d_keep (import); | |
54 | ||
55 | return import; | |
56 | } | |
57 | ||
58 | public: | |
59 | ImportVisitor (void) | |
60 | { | |
42d9ff3a IB |
61 | this->result_ = NULL_TREE; |
62 | } | |
63 | ||
64 | tree result (void) | |
65 | { | |
66 | return this->result_; | |
b4c522fa IB |
67 | } |
68 | ||
69 | /* This should be overridden by each symbol class. */ | |
329417d7 | 70 | void visit (Dsymbol *) final override |
b4c522fa IB |
71 | { |
72 | gcc_unreachable (); | |
73 | } | |
74 | ||
75 | /* Build the module decl for M, this is considered toplevel, regardless | |
76 | of whether there are any parent packages in the module system. */ | |
329417d7 | 77 | void visit (Module *m) final override |
b4c522fa IB |
78 | { |
79 | Loc loc = (m->md != NULL) ? m->md->loc | |
5fee5ec3 | 80 | : Loc (m->srcfile.toChars (), 1, 0); |
b4c522fa | 81 | |
42d9ff3a IB |
82 | this->result_ = build_decl (make_location_t (loc), NAMESPACE_DECL, |
83 | get_identifier (m->toPrettyChars ()), | |
84 | void_type_node); | |
85 | d_keep (this->result_); | |
b4c522fa IB |
86 | |
87 | if (!m->isRoot ()) | |
42d9ff3a | 88 | DECL_EXTERNAL (this->result_) = 1; |
b4c522fa | 89 | |
42d9ff3a IB |
90 | TREE_PUBLIC (this->result_) = 1; |
91 | DECL_CONTEXT (this->result_) = NULL_TREE; | |
b4c522fa IB |
92 | } |
93 | ||
94 | /* Build an import of another module symbol. */ | |
95 | ||
329417d7 | 96 | void visit (Import *m) final override |
b4c522fa IB |
97 | { |
98 | tree module = build_import_decl (m->mod); | |
42d9ff3a | 99 | this->result_ = this->make_import (module); |
b4c522fa IB |
100 | } |
101 | ||
102 | /* Build an import for any kind of user defined type. | |
103 | Use the TYPE_DECL associated with the type symbol. */ | |
329417d7 | 104 | void visit (EnumDeclaration *d) final override |
b4c522fa IB |
105 | { |
106 | tree type = build_ctype (d->type); | |
107 | /* Not all kinds of D enums create a TYPE_DECL. */ | |
108 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
7e287503 | 109 | this->result_ = this->make_import (TYPE_STUB_DECL (type)); |
b4c522fa IB |
110 | } |
111 | ||
329417d7 | 112 | void visit (AggregateDeclaration *d) final override |
b4c522fa IB |
113 | { |
114 | tree type = build_ctype (d->type); | |
7e287503 | 115 | this->result_ = this->make_import (TYPE_STUB_DECL (type)); |
b4c522fa IB |
116 | } |
117 | ||
329417d7 | 118 | void visit (ClassDeclaration *d) final override |
b4c522fa IB |
119 | { |
120 | /* Want the RECORD_TYPE, not POINTER_TYPE. */ | |
121 | tree type = TREE_TYPE (build_ctype (d->type)); | |
7e287503 | 122 | this->result_ = this->make_import (TYPE_STUB_DECL (type)); |
b4c522fa IB |
123 | } |
124 | ||
125 | /* For now, ignore importing other kinds of dsymbols. */ | |
329417d7 | 126 | void visit (ScopeDsymbol *) final override |
b4c522fa IB |
127 | { |
128 | } | |
129 | ||
130 | /* Alias symbols aren't imported, but their targets are. */ | |
329417d7 | 131 | void visit (AliasDeclaration *d) final override |
b4c522fa IB |
132 | { |
133 | Dsymbol *dsym = d->toAlias (); | |
134 | ||
135 | if (dsym == d) | |
136 | { | |
137 | Type *type = d->getType (); | |
138 | ||
139 | /* Type imports should really be part of their own visit method. */ | |
140 | if (type != NULL) | |
141 | { | |
5fee5ec3 | 142 | if (type->ty == TY::Tenum) |
89fdaf5a | 143 | dsym = type->isTypeEnum ()->sym; |
5fee5ec3 | 144 | else if (type->ty == TY::Tstruct) |
89fdaf5a | 145 | dsym = type->isTypeStruct ()->sym; |
5fee5ec3 | 146 | else if (type->ty == TY::Tclass) |
89fdaf5a | 147 | dsym = type->isTypeClass ()->sym; |
b4c522fa IB |
148 | } |
149 | } | |
150 | ||
151 | /* This symbol is really an alias for another, visit the other. */ | |
152 | if (dsym != d) | |
42d9ff3a | 153 | dsym->accept (this); |
b4c522fa IB |
154 | } |
155 | ||
156 | /* Visit the underlying alias symbol of overloadable aliases. */ | |
329417d7 | 157 | void visit (OverDeclaration *d) final override |
b4c522fa IB |
158 | { |
159 | if (d->aliassym != NULL) | |
42d9ff3a | 160 | d->aliassym->accept (this); |
b4c522fa IB |
161 | } |
162 | ||
163 | /* Function aliases are the same as alias symbols. */ | |
329417d7 | 164 | void visit (FuncAliasDeclaration *d) final override |
b4c522fa IB |
165 | { |
166 | FuncDeclaration *fd = d->toAliasFunc (); | |
167 | ||
168 | if (fd != NULL) | |
42d9ff3a | 169 | fd->accept (this); |
b4c522fa IB |
170 | } |
171 | ||
172 | /* Skip over importing templates and tuples. */ | |
329417d7 | 173 | void visit (TemplateDeclaration *) final override |
b4c522fa IB |
174 | { |
175 | } | |
176 | ||
329417d7 | 177 | void visit (TupleDeclaration *) final override |
b4c522fa IB |
178 | { |
179 | } | |
180 | ||
181 | /* Import any other kind of declaration. If the class does not implement | |
182 | symbol generation routines, the compiler will throw an error. */ | |
329417d7 | 183 | void visit (Declaration *d) final override |
b4c522fa | 184 | { |
42d9ff3a | 185 | this->result_ = this->make_import (get_symbol_decl (d)); |
b4c522fa IB |
186 | } |
187 | }; | |
188 | ||
189 | ||
190 | /* Build a declaration for the symbol D that can be used for the | |
191 | debug_hook imported_module_or_decl. */ | |
192 | tree | |
193 | build_import_decl (Dsymbol *d) | |
194 | { | |
42d9ff3a IB |
195 | hash_map_maybe_create<hm_ggc> (imported_decls); |
196 | ||
197 | if (tree *decl = imported_decls->get (d)) | |
198 | return *decl; | |
b4c522fa | 199 | |
42d9ff3a IB |
200 | location_t saved_location = input_location; |
201 | ImportVisitor v = ImportVisitor (); | |
202 | ||
203 | input_location = make_location_t (d->loc); | |
204 | d->accept (&v); | |
205 | input_location = saved_location; | |
206 | ||
207 | /* Not all visitors set `result'. */ | |
208 | tree isym = v.result (); | |
209 | if (isym != NULL_TREE) | |
210 | imported_decls->put (d, isym); | |
211 | ||
212 | return isym; | |
213 | } |