libgo: permit loff_t and off_t to be macros
[official-gcc.git] / gcc / go / gofrontend / embed.cc
blob0584f707ce6bac47dce95e1b7c5a29bdf81688d5
1 // embed.cc -- Go frontend go:embed handling.
3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #include "go-system.h"
9 #include "operator.h"
10 #include "go-diagnostics.h"
11 #include "lex.h"
12 #include "types.h"
13 #include "expressions.h"
14 #include "gogo.h"
16 #ifndef O_BINARY
17 #define O_BINARY 0
18 #endif
20 // Read a file into *DATA. Returns false on error.
22 static bool
23 read_file(const char* filename, Location loc, std::string* data)
25 int fd = open(filename, O_RDONLY | O_BINARY);
26 if (fd < 0)
28 go_error_at(loc, "%s: %m", filename);
29 return false;
32 struct stat st;
33 if (fstat(fd, &st) < 0)
35 go_error_at(loc, "%s: %m", filename);
36 return false;
38 off_t want = st.st_size;
40 // Most files read here are going to be incorporated into the object file
41 // and then the executable. Set a limit on the size we will accept.
42 if (want > 2000000000)
44 go_error_at(loc, "%s: file too large", filename);
45 return false;
48 data->resize(want);
49 off_t got = 0;
50 while (want > 0)
52 // C++11 requires that std::string use contiguous bytes, so this
53 // is safe.
54 ssize_t n = read(fd, &(*data)[got], want);
55 if (n < 0)
57 close(fd);
58 go_error_at(loc, "%s: %m", filename);
59 return false;
61 if (n == 0)
63 data->resize(got);
64 break;
66 got += n;
67 want -= n;
70 close(fd);
71 return true;
74 // A JSON value as read from an embedcfg file. For our purposes a
75 // JSON value is a string, or a list of strings, or a mapping from
76 // strings to values. We don't expect any numbers. We also don't
77 // expect an array of anything other than strings; that is, we don't
78 // accept an array of general JSON values.
80 class Json_value
82 public:
83 // The types of values.
84 enum Json_value_classification
86 JSON_VALUE_UNKNOWN,
87 JSON_VALUE_STRING,
88 JSON_VALUE_ARRAY,
89 JSON_VALUE_MAP
92 Json_value()
93 : classification_(JSON_VALUE_UNKNOWN), string_(), array_(), map_()
94 { }
96 ~Json_value();
98 Json_value_classification
99 classification() const
100 { return this->classification_; }
102 // Set to a string value.
103 void
104 set_string(const std::string& str)
106 go_assert(this->classification_ == JSON_VALUE_UNKNOWN);
107 this->classification_ = JSON_VALUE_STRING;
108 this->string_ = str;
111 // Start an array value.
112 void
113 start_array()
115 go_assert(this->classification_ == JSON_VALUE_UNKNOWN);
116 this->classification_ = JSON_VALUE_ARRAY;
119 // Add an array entry.
120 void
121 add_array_entry(const std::string& s)
123 go_assert(this->classification_ == JSON_VALUE_ARRAY);
124 this->array_.push_back(s);
127 // Start a map value.
128 void
129 start_map()
131 go_assert(this->classification_ == JSON_VALUE_UNKNOWN);
132 this->classification_ = JSON_VALUE_MAP;
135 // Add a map entry.
136 void
137 add_map_entry(const std::string& key, Json_value* val)
139 go_assert(this->classification_ == JSON_VALUE_MAP);
140 this->map_[key] = val;
143 // Return the strings from a string value.
144 const std::string&
145 to_string() const
147 go_assert(this->classification_ == JSON_VALUE_STRING);
148 return this->string_;
151 // Fetch a vector of strings, and drop them from the JSON value.
152 void
153 get_and_clear_array(std::vector<std::string>* v)
155 go_assert(this->classification_ == JSON_VALUE_ARRAY);
156 std::swap(*v, this->array_);
159 // Look up a map entry. Returns NULL if not found.
160 Json_value*
161 lookup_map_entry(const std::string& key);
163 // Iterate over a map.
164 typedef Unordered_map(std::string, Json_value*)::iterator map_iterator;
166 map_iterator
167 map_begin()
169 go_assert(this->classification_ == JSON_VALUE_MAP);
170 return this->map_.begin();
173 map_iterator
174 map_end()
175 { return this->map_.end(); }
177 private:
178 // Classification.
179 Json_value_classification classification_;
180 // A string, for JSON_VALUE_STRING.
181 std::string string_;
182 // Array, for JSON_VALUE_ARRAY.
183 std::vector<std::string> array_;
184 // Mapping, for JSON_VALUE_MAP.
185 Unordered_map(std::string, Json_value*) map_;
188 // Delete a JSON value.
190 Json_value::~Json_value()
192 if (this->classification_ == JSON_VALUE_MAP)
194 for (map_iterator p = this->map_begin();
195 p != this->map_end();
196 ++p)
197 delete p->second;
201 // Look up a map entry in a JSON value.
203 Json_value*
204 Json_value::lookup_map_entry(const std::string& key)
206 go_assert(this->classification_ == JSON_VALUE_MAP);
207 Unordered_map(std::string, Json_value*)::iterator p = this->map_.find(key);
208 if (p == this->map_.end())
209 return NULL;
210 return p->second;
213 // Manage reading the embedcfg file.
215 class Embedcfg_reader
217 public:
218 Embedcfg_reader(const char* filename)
219 : filename_(filename), data_(), p_(NULL), pend_(NULL)
222 // Read the contents of FILENAME. Return whether it succeeded.
223 bool
224 initialize_from_file();
226 // Read a JSON object.
227 bool
228 read_object(Json_value*);
230 // Report an error if not at EOF.
231 void
232 check_eof();
234 // Report an error for the embedcfg file.
235 void
236 error(const char* msg);
238 private:
239 bool
240 read_value(Json_value*);
242 bool
243 read_array(Json_value*);
245 bool
246 read_string(std::string*);
248 bool
249 skip_whitespace(bool eof_ok);
251 // File name.
252 const char* filename_;
253 // File contents.
254 std::string data_;
255 // Next character to process.
256 const char *p_;
257 // End of data.
258 const char *pend_;
261 // Read the embedcfg file.
263 void
264 Gogo::read_embedcfg(const char *filename)
266 class Embedcfg_reader r(filename);
267 if (!r.initialize_from_file())
268 return;
270 Json_value val;
271 if (!r.read_object(&val))
272 return;
274 r.check_eof();
276 if (val.classification() != Json_value::JSON_VALUE_MAP)
278 r.error("invalid embedcfg: not a JSON object");
279 return;
282 Json_value* patterns = val.lookup_map_entry("Patterns");
283 if (patterns == NULL)
285 r.error("invalid embedcfg: missing Patterns");
286 return;
288 if (patterns->classification() != Json_value::JSON_VALUE_MAP)
290 r.error("invalid embedcfg: Patterns is not a JSON object");
291 return;
294 Json_value* files = val.lookup_map_entry("Files");
295 if (files == NULL)
297 r.error("invalid embedcfg: missing Files");
298 return;
300 if (files->classification() != Json_value::JSON_VALUE_MAP)
302 r.error("invalid embedcfg: Files is not a JSON object");
303 return;
306 for (Json_value::map_iterator p = patterns->map_begin();
307 p != patterns->map_end();
308 ++p)
310 if (p->second->classification() != Json_value::JSON_VALUE_ARRAY)
312 r.error("invalid embedcfg: Patterns entry is not an array");
313 return;
315 std::vector<std::string> files;
316 p->second->get_and_clear_array(&files);
318 std::pair<std::string, std::vector<std::string> > val;
319 val.first = p->first;
320 std::pair<Embed_patterns::iterator, bool> ins =
321 this->embed_patterns_.insert(val);
322 if (!ins.second)
324 r.error("invalid embedcfg: duplicate Patterns entry");
325 return;
327 std::swap(ins.first->second, files);
330 for (Json_value::map_iterator p = files->map_begin();
331 p != files->map_end();
332 ++p)
334 if (p->second->classification() != Json_value::JSON_VALUE_STRING)
336 r.error("invalid embedcfg: Files entry is not a string");
337 return;
339 this->embed_files_[p->first] = p->second->to_string();
343 // Read the contents of FILENAME into this->data_. Returns whether it
344 // succeeded.
346 bool
347 Embedcfg_reader::initialize_from_file()
349 if (!read_file(this->filename_, Linemap::unknown_location(), &this->data_))
350 return false;
351 if (this->data_.empty())
353 this->error("empty file");
354 return false;
356 this->p_ = this->data_.data();
357 this->pend_ = this->p_ + this->data_.size();
358 return true;
361 // Read a JSON object into VAL. Return whether it succeeded.
363 bool
364 Embedcfg_reader::read_object(Json_value* val)
366 if (!this->skip_whitespace(false))
367 return false;
368 if (*this->p_ != '{')
370 this->error("expected %<{%>");
371 return false;
373 ++this->p_;
375 val->start_map();
377 if (!this->skip_whitespace(false))
378 return false;
379 if (*this->p_ == '}')
381 ++this->p_;
382 return true;
385 while (true)
387 if (!this->skip_whitespace(false))
388 return false;
389 if (*this->p_ != '"')
391 this->error("expected %<\"%>");
392 return false;
395 std::string key;
396 if (!this->read_string(&key))
397 return false;
399 if (!this->skip_whitespace(false))
400 return false;
401 if (*this->p_ != ':')
403 this->error("expected %<:%>");
404 return false;
406 ++this->p_;
408 Json_value* subval = new Json_value();
409 if (!this->read_value(subval))
410 return false;
412 val->add_map_entry(key, subval);
414 if (!this->skip_whitespace(false))
415 return false;
416 if (*this->p_ == '}')
418 ++this->p_;
419 return true;
421 if (*this->p_ != ',')
423 this->error("expected %<,%> or %<}%>");
424 return false;
426 ++this->p_;
430 // Read a JSON array into VAL. Return whether it succeeded.
432 bool
433 Embedcfg_reader::read_array(Json_value* val)
435 if (!this->skip_whitespace(false))
436 return false;
437 if (*this->p_ != '[')
439 this->error("expected %<[%>");
440 return false;
442 ++this->p_;
444 val->start_array();
446 if (!this->skip_whitespace(false))
447 return false;
448 if (*this->p_ == ']')
450 ++this->p_;
451 return true;
454 while (true)
456 // If we were parsing full JSON we would call read_value here,
457 // not read_string.
459 std::string s;
460 if (!this->read_string(&s))
461 return false;
463 val->add_array_entry(s);
465 if (!this->skip_whitespace(false))
466 return false;
467 if (*this->p_ == ']')
469 ++this->p_;
470 return true;
472 if (*this->p_ != ',')
474 this->error("expected %<,%> or %<]%>");
475 return false;
477 ++this->p_;
481 // Read a JSON value into VAL. Return whether it succeeded.
483 bool
484 Embedcfg_reader::read_value(Json_value* val)
486 if (!this->skip_whitespace(false))
487 return false;
488 switch (*this->p_)
490 case '"':
492 std::string s;
493 if (!this->read_string(&s))
494 return false;
495 val->set_string(s);
496 return true;
499 case '{':
500 return this->read_object(val);
502 case '[':
503 return this->read_array(val);
505 default:
506 this->error("invalid JSON syntax");
507 return false;
511 // Read a JSON string. Return whether it succeeded.
513 bool
514 Embedcfg_reader::read_string(std::string* str)
516 if (!this->skip_whitespace(false))
517 return false;
518 if (*this->p_ != '"')
520 this->error("expected %<\"%>");
521 return false;
523 ++this->p_;
525 str->clear();
526 while (this->p_ < this->pend_ && *this->p_ != '"')
528 if (*this->p_ != '\\')
530 str->push_back(*this->p_);
531 ++this->p_;
532 continue;
535 ++this->p_;
536 if (this->p_ >= this->pend_)
538 this->error("unterminated string");
539 return false;
541 switch (*this->p_)
543 case '"': case '\\': case '/':
544 str->push_back(*this->p_);
545 ++this->p_;
546 break;
548 case 'b':
549 str->push_back('\b');
550 ++this->p_;
551 break;
553 case 'f':
554 str->push_back('\f');
555 ++this->p_;
556 break;
558 case 'n':
559 str->push_back('\n');
560 ++this->p_;
561 break;
563 case 'r':
564 str->push_back('\r');
565 ++this->p_;
566 break;
568 case 't':
569 str->push_back('\t');
570 ++this->p_;
571 break;
573 case 'u':
575 ++this->p_;
576 unsigned int rune = 0;
577 for (int i = 0; i < 4; i++)
579 if (this->p_ >= this->pend_)
581 this->error("unterminated string");
582 return false;
584 unsigned char c = *this->p_;
585 ++this->p_;
586 rune <<= 4;
587 if (c >= '0' && c <= '9')
588 rune += c - '0';
589 else if (c >= 'A' && c <= 'F')
590 rune += c - 'A' + 10;
591 else if (c >= 'a' && c <= 'f')
592 rune += c - 'a' + 10;
593 else
595 this->error("invalid hex digit");
596 return false;
599 Lex::append_char(rune, false, str, Linemap::unknown_location());
601 break;
603 default:
604 this->error("unrecognized string escape");
605 return false;
609 if (*this->p_ == '"')
611 ++this->p_;
612 return true;
615 this->error("unterminated string");
616 return false;
619 // Report an error if not at EOF.
621 void
622 Embedcfg_reader::check_eof()
624 if (this->skip_whitespace(true))
625 this->error("extraneous data at end of file");
628 // Skip whitespace. Return whether there is more to read.
630 bool
631 Embedcfg_reader::skip_whitespace(bool eof_ok)
633 while (this->p_ < this->pend_)
635 switch (*this->p_)
637 case ' ': case '\t': case '\n': case '\r':
638 ++this->p_;
639 break;
640 default:
641 return true;
644 if (!eof_ok)
645 this->error("unexpected EOF");
646 return false;
649 // Report an error.
651 void
652 Embedcfg_reader::error(const char* msg)
654 if (!this->data_.empty() && this->p_ != NULL)
655 go_error_at(Linemap::unknown_location(),
656 "%<-fgo-embedcfg%>: %s: %lu: %s",
657 this->filename_,
658 static_cast<unsigned long>(this->p_ - this->data_.data()),
659 msg);
660 else
661 go_error_at(Linemap::unknown_location(),
662 "%<-fgo-embedcfg%>: %s: %s",
663 this->filename_, msg);
666 // Implement the sort order for a list of embedded files, as discussed
667 // at the docs for embed.FS.
669 class Embedfs_sort
671 public:
672 bool
673 operator()(const std::string& p1, const std::string& p2) const;
675 private:
676 void
677 split(const std::string&, size_t*, size_t*, size_t*) const;
680 bool
681 Embedfs_sort::operator()(const std::string& p1, const std::string& p2) const
683 size_t dirlen1, elem1, elemlen1;
684 this->split(p1, &dirlen1, &elem1, &elemlen1);
685 size_t dirlen2, elem2, elemlen2;
686 this->split(p2, &dirlen2, &elem2, &elemlen2);
688 if (dirlen1 == 0)
690 if (dirlen2 > 0)
692 int i = p2.compare(0, dirlen2, ".");
693 if (i != 0)
694 return i > 0;
697 else if (dirlen2 == 0)
699 int i = p1.compare(0, dirlen1, ".");
700 if (i != 0)
701 return i < 0;
703 else
705 int i = p1.compare(0, dirlen1, p2, 0, dirlen2);
706 if (i != 0)
707 return i < 0;
710 int i = p1.compare(elem1, elemlen1, p2, elem2, elemlen2);
711 return i < 0;
714 // Pick out the directory and file name components for comparison.
716 void
717 Embedfs_sort::split(const std::string& s, size_t* dirlen, size_t* elem,
718 size_t* elemlen) const
720 size_t len = s.size();
721 if (len > 0 && s[len - 1] == '/')
722 --len;
723 size_t slash = s.rfind('/', len - 1);
724 if (slash == std::string::npos)
726 *dirlen = 0;
727 *elem = 0;
728 *elemlen = len;
730 else
732 *dirlen = slash;
733 *elem = slash + 1;
734 *elemlen = len - (slash + 1);
738 // Convert the go:embed directives for a variable into an initializer
739 // for that variable.
741 Expression*
742 Gogo::initializer_for_embeds(Type* type,
743 const std::vector<std::string>* embeds,
744 Location loc)
746 if (this->embed_patterns_.empty())
748 go_error_at(loc,
749 ("invalid go:embed: build system did not "
750 "supply embed configuration"));
751 return Expression::make_error(loc);
754 type = type->unalias();
756 enum {
757 EMBED_STRING = 0,
758 EMBED_BYTES = 1,
759 EMBED_FS = 2
760 } embed_kind;
762 const Named_type* nt = type->named_type();
763 if (nt != NULL
764 && nt->named_object()->package() != NULL
765 && nt->named_object()->package()->pkgpath() == "embed"
766 && nt->name() == "FS")
767 embed_kind = EMBED_FS;
768 else if (type->is_string_type())
769 embed_kind = EMBED_STRING;
770 else if (type->is_slice_type()
771 && type->array_type()->element_type()->integer_type() != NULL
772 && type->array_type()->element_type()->integer_type()->is_byte())
773 embed_kind = EMBED_BYTES;
774 else
776 go_error_at(loc, "invalid type for go:embed");
777 return Expression::make_error(loc);
780 // The patterns in the go:embed directive(s) are in EMBEDS. Find
781 // them in the patterns in the embedcfg file.
783 Unordered_set(std::string) have;
784 std::vector<std::string> paths;
785 for (std::vector<std::string>::const_iterator pe = embeds->begin();
786 pe != embeds->end();
787 pe++)
789 Embed_patterns::const_iterator pp = this->embed_patterns_.find(*pe);
790 if (pp == this->embed_patterns_.end())
792 go_error_at(loc,
793 ("invalid go:embed: build system did not "
794 "map pattern %<%s%>"),
795 pe->c_str());
796 continue;
799 // Each pattern in the embedcfg file maps to a list of file
800 // names. Add those file names to PATHS.
801 for (std::vector<std::string>::const_iterator pf = pp->second.begin();
802 pf != pp->second.end();
803 pf++)
805 if (this->embed_files_.find(*pf) == this->embed_files_.end())
807 go_error_at(loc,
808 ("invalid go:embed: build system did not "
809 "map file %<%s%>"),
810 pf->c_str());
811 continue;
814 std::pair<Unordered_set(std::string)::iterator, bool> ins
815 = have.insert(*pf);
816 if (ins.second)
818 const std::string& path(*pf);
819 paths.push_back(path);
821 if (embed_kind == EMBED_FS)
823 // Add each required directory, with a trailing slash.
824 size_t i = std::string::npos;
825 while (i > 0)
827 i = path.rfind('/', i);
828 if (i == std::string::npos)
829 break;
830 std::string dir = path.substr(0, i + 1);
831 ins = have.insert(dir);
832 if (ins.second)
833 paths.push_back(dir);
834 --i;
841 if (embed_kind == EMBED_STRING || embed_kind == EMBED_BYTES)
843 if (paths.size() > 1)
845 go_error_at(loc,
846 ("invalid go:embed: multiple files for "
847 "string or byte slice"));;
848 return Expression::make_error(loc);
851 std::string data;
852 if (!read_file(this->embed_files_[paths[0]].c_str(), loc, &data))
853 return Expression::make_error(loc);
855 Expression* e = Expression::make_string(data, loc);
856 if (embed_kind == EMBED_BYTES)
857 e = Expression::make_cast(type, e, loc);
858 return e;
861 std::sort(paths.begin(), paths.end(), Embedfs_sort());
863 if (type->struct_type() == NULL
864 || type->struct_type()->field_count() != 1)
866 go_error_at(loc,
867 ("internal error: embed.FS should be struct type "
868 "with one field"));
869 return Expression::make_error(loc);
872 Type* ptr_type = type->struct_type()->field(0)->type();
873 if (ptr_type->points_to() == NULL)
875 go_error_at(loc,
876 "internal error: embed.FS struct field should be pointer");
877 return Expression::make_error(loc);
880 Type* slice_type = ptr_type->points_to();
881 if (!slice_type->is_slice_type())
883 go_error_at(loc,
884 ("internal error: embed.FS struct field should be "
885 "pointer to slice"));
886 return Expression::make_error(loc);
889 Type* file_type = slice_type->array_type()->element_type();
890 if (file_type->struct_type() == NULL
891 || (file_type->struct_type()->find_local_field(".embed.name", NULL)
892 == NULL)
893 || (file_type->struct_type()->find_local_field(".embed.data", NULL)
894 == NULL))
896 go_error_at(loc,
897 ("internal error: embed.FS slice element should be struct "
898 "with name and data fields"));
899 return Expression::make_error(loc);
902 const Struct_field_list* file_fields = file_type->struct_type()->fields();
903 Expression_list* file_vals = new(Expression_list);
904 file_vals->reserve(paths.size());
905 for (std::vector<std::string>::const_iterator pp = paths.begin();
906 pp != paths.end();
907 ++pp)
909 std::string data;
910 if ((*pp)[pp->size() - 1] != '/')
912 if (!read_file(this->embed_files_[*pp].c_str(), loc, &data))
913 return Expression::make_error(loc);
916 Expression_list* field_vals = new(Expression_list);
917 for (Struct_field_list::const_iterator pf = file_fields->begin();
918 pf != file_fields->end();
919 ++pf)
921 if (pf->is_field_name(".embed.name"))
922 field_vals->push_back(Expression::make_string(*pp, loc));
923 else if (pf->is_field_name(".embed.data"))
924 field_vals->push_back(Expression::make_string(data, loc));
925 else
927 // FIXME: The embed.file type has a hash field, which is
928 // currently unused. We should fill it in, but don't.
929 // The hash is a SHA256, and we don't have convenient
930 // SHA256 code. Do this later when the field is
931 // actually used.
932 field_vals->push_back(NULL);
936 Expression* file_val =
937 Expression::make_struct_composite_literal(file_type, field_vals, loc);
938 file_vals->push_back(file_val);
941 Expression* slice_init =
942 Expression::make_slice_composite_literal(slice_type, file_vals, loc);
943 Expression* fs_init = Expression::make_heap_expression(slice_init, loc);
944 Expression_list* fs_vals = new Expression_list();
945 fs_vals->push_back(fs_init);
946 return Expression::make_struct_composite_literal(type, fs_vals, loc);