1 /* d-frontend.cc -- D frontend interface to the gcc back-end.
2    Copyright (C) 2013-2021 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 "options.h"
31 #include "fold-const.h"
32 #include "diagnostic.h"
33 
34 #include "d-tree.h"
35 
36 
37 /* Implements the Global interface defined by the frontend.
38    Used for managing the state of the current compilation.  */
39 
40 Global global;
41 
42 void
_init(void)43 Global::_init (void)
44 {
45   this->mars_ext = "d";
46   this->hdr_ext  = "di";
47   this->doc_ext  = "html";
48   this->ddoc_ext = "ddoc";
49   this->json_ext = "json";
50   this->obj_ext = "o";
51 
52   this->run_noext = true;
53   this->version = "v"
54 #include "verstr.h"
55     ;
56 
57   this->stdmsg = stderr;
58 }
59 
60 /* Start gagging. Return the current number of gagged errors.  */
61 
62 unsigned
startGagging(void)63 Global::startGagging (void)
64 {
65   this->gag++;
66   return this->gaggedErrors;
67 }
68 
69 /* End gagging, restoring the old gagged state.  Return true if errors
70    occured while gagged.  */
71 
72 bool
endGagging(unsigned oldGagged)73 Global::endGagging (unsigned oldGagged)
74 {
75   bool anyErrs = (this->gaggedErrors != oldGagged);
76   this->gag--;
77 
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;
82 
83   return anyErrs;
84 }
85 
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.  */
88 
89 void
increaseErrorCount(void)90 Global::increaseErrorCount (void)
91 {
92   if (gag)
93     this->gaggedErrors++;
94 
95   this->errors++;
96 }
97 
98 
99 /* Implements the Loc interface defined by the frontend.
100    Used for keeping track of current file/line position in code.  */
101 
Loc(const char * filename,unsigned linnum,unsigned charnum)102 Loc::Loc (const char *filename, unsigned linnum, unsigned charnum)
103 {
104   this->linnum = linnum;
105   this->charnum = charnum;
106   this->filename = filename;
107 }
108 
109 const char *
toChars(void) const110 Loc::toChars (void) const
111 {
112   OutBuffer buf;
113 
114   if (this->filename)
115     buf.printf ("%s", this->filename);
116 
117   if (this->linnum)
118     {
119       buf.printf (":%u", this->linnum);
120       if (this->charnum)
121 	buf.printf (":%u", this->charnum);
122     }
123 
124   return buf.extractChars ();
125 }
126 
127 bool
equals(const Loc & loc)128 Loc::equals (const Loc &loc)
129 {
130   if (this->linnum != loc.linnum || this->charnum != loc.charnum)
131     return false;
132 
133   if (!FileName::equals (this->filename, loc.filename))
134     return false;
135 
136   return true;
137 }
138 
139 
140 /* Implements back-end specific interfaces used by the frontend.  */
141 
142 /* Determine if function FD is a builtin one that we can evaluate in CTFE.  */
143 
144 BUILTIN
isBuiltin(FuncDeclaration * fd)145 isBuiltin (FuncDeclaration *fd)
146 {
147   if (fd->builtin != BUILTINunknown)
148     return fd->builtin;
149 
150   maybe_set_intrinsic (fd);
151 
152   return fd->builtin;
153 }
154 
155 /* Evaluate builtin D function FD whose argument list is ARGUMENTS.
156    Return result; NULL if cannot evaluate it.  */
157 
158 Expression *
eval_builtin(Loc loc,FuncDeclaration * fd,Expressions * arguments)159 eval_builtin (Loc loc, FuncDeclaration *fd, Expressions *arguments)
160 {
161   if (fd->builtin == BUILTINunimp)
162     return NULL;
163 
164   tree decl = get_symbol_decl (fd);
165   gcc_assert (fndecl_built_in_p (decl)
166 	      || DECL_INTRINSIC_CODE (decl) != INTRINSIC_NONE);
167 
168   TypeFunction *tf = fd->type->toTypeFunction ();
169   Expression *e = NULL;
170   input_location = make_location_t (loc);
171 
172   tree result = d_build_call (tf, decl, NULL, arguments);
173   result = fold (result);
174 
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);
179 
180   return e;
181 }
182 
183 /* Build and return typeinfo type for TYPE.  */
184 
185 Type *
getTypeInfoType(Loc loc,Type * type,Scope * sc)186 getTypeInfoType (Loc loc, Type *type, Scope *sc)
187 {
188   gcc_assert (type->ty != Terror);
189   check_typeinfo_type (loc, sc);
190   create_typeinfo (type, sc ? sc->_module->importedFrom : NULL);
191   return type->vtinfo->type;
192 }
193 
194 /* Return an inlined copy of a default argument for a function parameter.  */
195 
196 Expression *
inlineCopy(Expression * e,Scope *)197 inlineCopy (Expression *e, Scope *)
198 {
199   return e->copy ();
200 }
201