1 /* C++ modules. Experimental! -*- c++ -*-
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
31 #if 0 // 1 for testing no mmap
32 #define MAPPED_READING 0
35 #if HAVE_MMAP_FILE && _POSIX_MAPPED_FILES > 0
36 #define MAPPED_READING 1
38 #define MAPPED_READING 0
41 #ifdef HAVE_SYS_MMAN_H
43 #define MAPPED_READING 1
45 #define MAPPED_READING 0
50 #include <sys/types.h>
53 #if !defined (IN_GCC) && !MAPPED_READING
54 #define xmalloc(X) malloc(X)
57 #if !HOST_HAS_O_CLOEXEC
62 #define DIR_SEPARATOR '/'
65 module_resolver::module_resolver (bool map
, bool xlate
)
66 : default_map (map
), default_translate (xlate
)
70 module_resolver::~module_resolver ()
77 module_resolver::set_repo (std::string
&&r
, bool force
)
79 if (force
|| repo
.empty ())
88 module_resolver::add_mapping (std::string
&&module
, std::string
&&file
,
91 auto res
= map
.emplace (std::move (module
), std::move (file
));
95 res
.first
->second
= std::move (file
);
101 module_resolver::read_tuple_file (int fd
, char const *prefix
, bool force
)
104 if (fstat (fd
, &stat
) < 0)
110 void *buffer
= nullptr;
112 // Just map the file, we're gonna read all of it, so no need for
114 buffer
= mmap (nullptr, stat
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
115 if (buffer
== MAP_FAILED
)
118 buffer
= xmalloc (stat
.st_size
);
121 if (read (fd
, buffer
, stat
.st_size
) != stat
.st_size
)
125 size_t prefix_len
= prefix
? strlen (prefix
) : 0;
128 for (char const *begin
= reinterpret_cast <char const *> (buffer
),
129 *end
= begin
+ stat
.st_size
, *eol
;
130 begin
!= end
; begin
= eol
+ 1)
133 eol
= std::find (begin
, end
, '\n');
135 // last line has no \n, ignore the line, you lose
139 bool pfx_search
= prefix_len
!= 0;
142 while (*pos
== ' ' || *pos
== '\t')
146 while (*space
!= '\n' && *space
!= ' ' && *space
!= '\t')
150 // at end of line, nothing here
155 if (size_t (space
- pos
) == prefix_len
156 && std::equal (pos
, space
, prefix
))
162 std::string
module (pos
, space
);
163 while (*space
== ' ' || *space
== '\t')
165 std::string
file (space
, eol
);
167 if (module
[0] == '$')
169 if (module
== "$root")
170 set_repo (std::move (file
));
177 file
= GetCMIName (module
);
178 add_mapping (std::move (module
), std::move (file
), force
);
183 munmap (buffer
, stat
.st_size
);
192 module_resolver::GetCMISuffix ()
198 module_resolver::ConnectRequest (Cody::Server
*s
, unsigned version
,
199 std::string
&a
, std::string
&i
)
201 if (!version
|| version
> Cody::Version
)
202 s
->ErrorResponse ("version mismatch");
204 // Refuse anything but GCC
205 ErrorResponse (s
, std::string ("only GCC supported"));
206 else if (!ident
.empty () && ident
!= i
)
207 // Failed ident check
208 ErrorResponse (s
, std::string ("bad ident"));
211 s
->ConnectResponse ("gcc");
217 module_resolver::ModuleRepoRequest (Cody::Server
*s
)
219 s
->PathnameResponse (repo
);
224 module_resolver::cmi_response (Cody::Server
*s
, std::string
&module
)
226 auto iter
= map
.find (module
);
227 if (iter
== map
.end ())
229 std::string file
= default_map
? GetCMIName (module
) : std::string ();
230 auto res
= map
.emplace (module
, file
);
234 if (iter
->second
.empty ())
235 s
->ErrorResponse ("no such module");
237 s
->PathnameResponse (iter
->second
);
243 module_resolver::ModuleExportRequest (Cody::Server
*s
, Cody::Flags
,
246 return cmi_response (s
, module
);
250 module_resolver::ModuleImportRequest (Cody::Server
*s
, Cody::Flags
,
253 return cmi_response (s
, module
);
257 module_resolver::IncludeTranslateRequest (Cody::Server
*s
, Cody::Flags
,
258 std::string
&include
)
260 auto iter
= map
.find (include
);
261 if (iter
== map
.end () && default_translate
)
263 // Not found, look for it
264 auto file
= GetCMIName (include
);
269 int fd_dir
= AT_FDCWD
;
274 fd_repo
= open (repo
.c_str (),
275 O_RDONLY
| O_CLOEXEC
| O_DIRECTORY
);
282 if (!repo
.empty () && fd_repo
< 0)
284 else if (fstatat (fd_dir
, file
.c_str (), &statbuf
, 0) < 0
285 || !S_ISREG (statbuf
.st_mode
))
289 append
.push_back (DIR_SEPARATOR
);
290 append
.append (file
);
291 if (stat (append
.c_str (), &statbuf
) < 0
292 || !S_ISREG (statbuf
.st_mode
))
296 // Mark as not present
298 auto res
= map
.emplace (include
, file
);
302 if (iter
== map
.end () || iter
->second
.empty ())
303 s
->BoolResponse (false);
305 s
->PathnameResponse (iter
->second
);