1 /* imports.cc -- Build imported modules/declarations.
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
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)
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.
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/>. */
20 #include "coretypes.h"
22 #include "dmd/aggregate.h"
23 #include "dmd/declaration.h"
25 #include "dmd/identifier.h"
26 #include "dmd/import.h"
27 #include "dmd/module.h"
30 #include "stringpool.h"
35 /* Implements the visitor interface to build debug trees for all
36 module and import declarations, where ISYM holds the cached
37 back-end representation to be returned. */
38 class ImportVisitor
: public Visitor
42 /* Build the declaration DECL as an imported symbol. */
43 tree
make_import (tree decl
)
45 gcc_assert (decl
!= NULL_TREE
);
47 tree import
= build_decl (input_location
, IMPORTED_DECL
,
48 DECL_NAME (decl
), void_type_node
);
49 IMPORTED_DECL_ASSOCIATED_DECL (import
) = decl
;
60 /* This should be overridden by each symbol class. */
61 void visit (Dsymbol
*)
66 /* Build the module decl for M, this is considered toplevel, regardless
67 of whether there are any parent packages in the module system. */
68 void visit (Module
*m
)
70 Loc loc
= (m
->md
!= NULL
) ? m
->md
->loc
71 : Loc (m
->srcfile
->toChars (), 1, 0);
73 m
->isym
= build_decl (make_location_t (loc
), NAMESPACE_DECL
,
74 get_identifier (m
->toPrettyChars ()),
79 DECL_EXTERNAL (m
->isym
) = 1;
81 TREE_PUBLIC (m
->isym
) = 1;
82 DECL_CONTEXT (m
->isym
) = NULL_TREE
;
85 /* Build an import of another module symbol. */
87 void visit (Import
*m
)
89 tree module
= build_import_decl (m
->mod
);
90 m
->isym
= this->make_import (module
);
93 /* Build an import for any kind of user defined type.
94 Use the TYPE_DECL associated with the type symbol. */
95 void visit (EnumDeclaration
*d
)
97 tree type
= build_ctype (d
->type
);
98 /* Not all kinds of D enums create a TYPE_DECL. */
99 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
100 d
->isym
= this->make_import (TYPE_STUB_DECL (type
));
103 void visit (AggregateDeclaration
*d
)
105 tree type
= build_ctype (d
->type
);
106 d
->isym
= this->make_import (TYPE_STUB_DECL (type
));
109 void visit (ClassDeclaration
*d
)
111 /* Want the RECORD_TYPE, not POINTER_TYPE. */
112 tree type
= TREE_TYPE (build_ctype (d
->type
));
113 d
->isym
= this->make_import (TYPE_STUB_DECL (type
));
116 /* For now, ignore importing other kinds of dsymbols. */
117 void visit (ScopeDsymbol
*)
121 /* Alias symbols aren't imported, but their targets are. */
122 void visit (AliasDeclaration
*d
)
124 Dsymbol
*dsym
= d
->toAlias ();
128 Type
*type
= d
->getType ();
130 /* Type imports should really be part of their own visit method. */
133 if (type
->ty
== Tenum
)
134 dsym
= type
->isTypeEnum ()->sym
;
135 else if (type
->ty
== Tstruct
)
136 dsym
= type
->isTypeStruct ()->sym
;
137 else if (type
->ty
== Tclass
)
138 dsym
= type
->isTypeClass ()->sym
;
142 /* This symbol is really an alias for another, visit the other. */
146 d
->isym
= dsym
->isym
;
150 /* Visit the underlying alias symbol of overloadable aliases. */
151 void visit (OverDeclaration
*d
)
153 if (d
->aliassym
!= NULL
)
155 d
->aliassym
->accept (this);
156 d
->isym
= d
->aliassym
->isym
;
160 /* Function aliases are the same as alias symbols. */
161 void visit (FuncAliasDeclaration
*d
)
163 FuncDeclaration
*fd
= d
->toAliasFunc ();
172 /* Skip over importing templates and tuples. */
173 void visit (TemplateDeclaration
*)
177 void visit (TupleDeclaration
*)
181 /* Import any other kind of declaration. If the class does not implement
182 symbol generation routines, the compiler will throw an error. */
183 void visit (Declaration
*d
)
185 d
->isym
= this->make_import (get_symbol_decl (d
));
190 /* Build a declaration for the symbol D that can be used for the
191 debug_hook imported_module_or_decl. */
193 build_import_decl (Dsymbol
*d
)
197 location_t saved_location
= input_location
;
200 input_location
= make_location_t (d
->loc
);
202 input_location
= saved_location
;
205 /* Not all visitors set `isym'. */
206 return d
->isym
? d
->isym
: NULL_TREE
;