1 /* d-frontend.cc -- D frontend interface to the gcc back-end.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
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/expression.h"
25 #include "dmd/module.h"
26 #include "dmd/mtype.h"
27 #include "dmd/scope.h"
28
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "diagnostic.h"
32
33 #include "d-tree.h"
34 #include "d-frontend.h"
35
36 /* Implements back-end specific interfaces used by the frontend. */
37
38 /* Determine if function FD is a builtin one that we can evaluate in CTFE. */
39
40 BUILTIN
isBuiltin(FuncDeclaration * fd)41 isBuiltin (FuncDeclaration *fd)
42 {
43 if (fd->builtin != BUILTIN::unknown)
44 return fd->builtin;
45
46 maybe_set_intrinsic (fd);
47
48 return fd->builtin;
49 }
50
51 /* Evaluate builtin D function FD whose argument list is ARGUMENTS.
52 Return result; NULL if cannot evaluate it. */
53
54 Expression *
eval_builtin(const Loc & loc,FuncDeclaration * fd,Expressions * arguments)55 eval_builtin (const Loc &loc, FuncDeclaration *fd, Expressions *arguments)
56 {
57 if (fd->builtin == BUILTIN::unimp)
58 return NULL;
59
60 tree decl = get_symbol_decl (fd);
61 gcc_assert (fndecl_built_in_p (decl)
62 || DECL_INTRINSIC_CODE (decl) != INTRINSIC_NONE);
63
64 TypeFunction *tf = fd->type->toTypeFunction ();
65 Expression *e = NULL;
66 input_location = make_location_t (loc);
67
68 tree result = d_build_call (tf, decl, NULL, arguments);
69 result = fold (result);
70
71 /* Builtin should be successfully evaluated.
72 Will only return NULL if we can't convert it. */
73 if (TREE_CONSTANT (result) && TREE_CODE (result) != CALL_EXPR)
74 e = d_eval_constant_expression (loc, result);
75
76 return e;
77 }
78
79 /* Build and return typeinfo type for TYPE. */
80
81 Type *
getTypeInfoType(const Loc & loc,Type * type,Scope * sc)82 getTypeInfoType (const Loc &loc, Type *type, Scope *sc)
83 {
84 gcc_assert (type->ty != TY::Terror);
85 check_typeinfo_type (loc, sc);
86 create_typeinfo (type, sc ? sc->_module->importedFrom : NULL);
87 return type->vtinfo->type;
88 }
89
90 void
toObjFile(Dsymbol * ds,bool)91 toObjFile (Dsymbol *ds, bool)
92 {
93 build_decl_tree (ds);
94 }
95