1 /* d-frontend.cc -- D frontend interface to the gcc back-end.
2 Copyright (C) 2013-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"
24 #include "dmd/expression.h"
25 #include "dmd/module.h"
26 #include "dmd/mtype.h"
27 #include "dmd/scope.h"
31 #include "fold-const.h"
32 #include "diagnostic.h"
37 /* Implements the Global interface defined by the frontend.
38 Used for managing the state of the current compilation. */
47 this->doc_ext
= "html";
48 this->ddoc_ext
= "ddoc";
49 this->json_ext
= "json";
52 this->run_noext
= true;
57 this->stdmsg
= stderr
;
60 /* Start gagging. Return the current number of gagged errors. */
63 Global::startGagging (void)
66 return this->gaggedErrors
;
69 /* End gagging, restoring the old gagged state. Return true if errors
70 occured while gagged. */
73 Global::endGagging (unsigned oldGagged
)
75 bool anyErrs
= (this->gaggedErrors
!= oldGagged
);
78 /* Restore the original state of gagged errors; set total errors
79 to be original errors + new ungagged errors. */
80 this->errors
-= (this->gaggedErrors
- oldGagged
);
81 this->gaggedErrors
= oldGagged
;
86 /* Increment the error count to record that an error has occured in the
87 current context. An error message may or may not have been printed. */
90 Global::increaseErrorCount (void)
99 /* Implements the Loc interface defined by the frontend.
100 Used for keeping track of current file/line position in code. */
102 Loc::Loc (const char *filename
, unsigned linnum
, unsigned charnum
)
104 this->linnum
= linnum
;
105 this->charnum
= charnum
;
106 this->filename
= filename
;
110 Loc::toChars (void) const
115 buf
.printf ("%s", this->filename
);
119 buf
.printf (":%u", this->linnum
);
121 buf
.printf (":%u", this->charnum
);
124 return buf
.extractChars ();
128 Loc::equals (const Loc
&loc
)
130 if (this->linnum
!= loc
.linnum
|| this->charnum
!= loc
.charnum
)
133 if (!FileName::equals (this->filename
, loc
.filename
))
140 /* Implements back-end specific interfaces used by the frontend. */
142 /* Determine if function FD is a builtin one that we can evaluate in CTFE. */
145 isBuiltin (FuncDeclaration
*fd
)
147 if (fd
->builtin
!= BUILTINunknown
)
150 maybe_set_intrinsic (fd
);
155 /* Evaluate builtin D function FD whose argument list is ARGUMENTS.
156 Return result; NULL if cannot evaluate it. */
159 eval_builtin (Loc loc
, FuncDeclaration
*fd
, Expressions
*arguments
)
161 if (fd
->builtin
!= BUILTINyes
)
164 tree decl
= get_symbol_decl (fd
);
165 gcc_assert (fndecl_built_in_p (decl
)
166 || DECL_INTRINSIC_CODE (decl
) != INTRINSIC_NONE
);
168 TypeFunction
*tf
= fd
->type
->toTypeFunction ();
169 Expression
*e
= NULL
;
170 input_location
= make_location_t (loc
);
172 tree result
= d_build_call (tf
, decl
, NULL
, arguments
);
173 result
= fold (result
);
175 /* Builtin should be successfully evaluated.
176 Will only return NULL if we can't convert it. */
177 if (TREE_CONSTANT (result
) && TREE_CODE (result
) != CALL_EXPR
)
178 e
= d_eval_constant_expression (loc
, result
);
183 /* Build and return typeinfo type for TYPE. */
186 getTypeInfoType (Loc loc
, Type
*type
, Scope
*sc
)
188 if (!global
.params
.useTypeInfo
)
190 /* Even when compiling without RTTI we should still be able to evaluate
191 TypeInfo at compile-time, just not at run-time. */
192 if (!sc
|| !(sc
->flags
& SCOPEctfe
))
194 static int warned
= 0;
198 error_at (make_location_t (loc
),
199 "%<object.TypeInfo%> cannot be used with %<-fno-rtti%>");
205 if (Type::dtypeinfo
== NULL
206 || (Type::dtypeinfo
->storage_class
& STCtemp
))
208 /* If TypeInfo has not been declared, warn about each location once. */
211 if (!loc
.equals (warnloc
))
213 error_at (make_location_t (loc
),
214 "%<object.TypeInfo%> could not be found, "
215 "but is implicitly used");
220 gcc_assert (type
->ty
!= Terror
);
221 create_typeinfo (type
, sc
? sc
->_module
->importedFrom
: NULL
);
222 return type
->vtinfo
->type
;
225 /* Return an inlined copy of a default argument for a function parameter. */
228 inlineCopy (Expression
*e
, Scope
*)