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.
10 #include "go-diagnostics.h"
13 #include "expressions.h"
20 // Read a file into *DATA. Returns false on error.
23 read_file(const char* filename
, Location loc
, std::string
* data
)
25 int fd
= open(filename
, O_RDONLY
| O_BINARY
);
28 go_error_at(loc
, "%s: %m", filename
);
33 if (fstat(fd
, &st
) < 0)
35 go_error_at(loc
, "%s: %m", filename
);
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
);
52 // C++11 requires that std::string use contiguous bytes, so this
54 ssize_t n
= read(fd
, &(*data
)[got
], want
);
58 go_error_at(loc
, "%s: %m", filename
);
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.
83 // The types of values.
84 enum Json_value_classification
93 : classification_(JSON_VALUE_UNKNOWN
), string_(), array_(), map_()
98 Json_value_classification
99 classification() const
100 { return this->classification_
; }
102 // Set to a string value.
104 set_string(const std::string
& str
)
106 go_assert(this->classification_
== JSON_VALUE_UNKNOWN
);
107 this->classification_
= JSON_VALUE_STRING
;
111 // Start an array value.
115 go_assert(this->classification_
== JSON_VALUE_UNKNOWN
);
116 this->classification_
= JSON_VALUE_ARRAY
;
119 // Add an array entry.
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.
131 go_assert(this->classification_
== JSON_VALUE_UNKNOWN
);
132 this->classification_
= JSON_VALUE_MAP
;
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.
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.
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.
161 lookup_map_entry(const std::string
& key
);
163 // Iterate over a map.
164 typedef Unordered_map(std::string
, Json_value
*)::iterator map_iterator
;
169 go_assert(this->classification_
== JSON_VALUE_MAP
);
170 return this->map_
.begin();
175 { return this->map_
.end(); }
179 Json_value_classification classification_
;
180 // A string, for JSON_VALUE_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();
201 // Look up a map entry in a 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())
213 // Manage reading the embedcfg file.
215 class Embedcfg_reader
218 Embedcfg_reader(const char* filename
)
219 : filename_(filename
), data_(), p_(NULL
), pend_(NULL
)
222 // Read the contents of FILENAME. Return whether it succeeded.
224 initialize_from_file();
226 // Read a JSON object.
228 read_object(Json_value
*);
230 // Report an error if not at EOF.
234 // Report an error for the embedcfg file.
236 error(const char* msg
);
240 read_value(Json_value
*);
243 read_array(Json_value
*);
246 read_string(std::string
*);
249 skip_whitespace(bool eof_ok
);
252 const char* filename_
;
255 // Next character to process.
261 // Read the embedcfg file.
264 Gogo::read_embedcfg(const char *filename
)
266 class Embedcfg_reader
r(filename
);
267 if (!r
.initialize_from_file())
271 if (!r
.read_object(&val
))
276 if (val
.classification() != Json_value::JSON_VALUE_MAP
)
278 r
.error("invalid embedcfg: not a JSON object");
282 Json_value
* patterns
= val
.lookup_map_entry("Patterns");
283 if (patterns
== NULL
)
285 r
.error("invalid embedcfg: missing Patterns");
288 if (patterns
->classification() != Json_value::JSON_VALUE_MAP
)
290 r
.error("invalid embedcfg: Patterns is not a JSON object");
294 Json_value
* files
= val
.lookup_map_entry("Files");
297 r
.error("invalid embedcfg: missing Files");
300 if (files
->classification() != Json_value::JSON_VALUE_MAP
)
302 r
.error("invalid embedcfg: Files is not a JSON object");
306 for (Json_value::map_iterator p
= patterns
->map_begin();
307 p
!= patterns
->map_end();
310 if (p
->second
->classification() != Json_value::JSON_VALUE_ARRAY
)
312 r
.error("invalid embedcfg: Patterns entry is not an array");
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
);
324 r
.error("invalid embedcfg: duplicate Patterns entry");
327 std::swap(ins
.first
->second
, files
);
330 for (Json_value::map_iterator p
= files
->map_begin();
331 p
!= files
->map_end();
334 if (p
->second
->classification() != Json_value::JSON_VALUE_STRING
)
336 r
.error("invalid embedcfg: Files entry is not a string");
339 this->embed_files_
[p
->first
] = p
->second
->to_string();
343 // Read the contents of FILENAME into this->data_. Returns whether it
347 Embedcfg_reader::initialize_from_file()
349 if (!read_file(this->filename_
, Linemap::unknown_location(), &this->data_
))
351 if (this->data_
.empty())
353 this->error("empty file");
356 this->p_
= this->data_
.data();
357 this->pend_
= this->p_
+ this->data_
.size();
361 // Read a JSON object into VAL. Return whether it succeeded.
364 Embedcfg_reader::read_object(Json_value
* val
)
366 if (!this->skip_whitespace(false))
368 if (*this->p_
!= '{')
370 this->error("expected %<{%>");
377 if (!this->skip_whitespace(false))
379 if (*this->p_
== '}')
387 if (!this->skip_whitespace(false))
389 if (*this->p_
!= '"')
391 this->error("expected %<\"%>");
396 if (!this->read_string(&key
))
399 if (!this->skip_whitespace(false))
401 if (*this->p_
!= ':')
403 this->error("expected %<:%>");
408 Json_value
* subval
= new Json_value();
409 if (!this->read_value(subval
))
412 val
->add_map_entry(key
, subval
);
414 if (!this->skip_whitespace(false))
416 if (*this->p_
== '}')
421 if (*this->p_
!= ',')
423 this->error("expected %<,%> or %<}%>");
430 // Read a JSON array into VAL. Return whether it succeeded.
433 Embedcfg_reader::read_array(Json_value
* val
)
435 if (!this->skip_whitespace(false))
437 if (*this->p_
!= '[')
439 this->error("expected %<[%>");
446 if (!this->skip_whitespace(false))
448 if (*this->p_
== ']')
456 // If we were parsing full JSON we would call read_value here,
460 if (!this->read_string(&s
))
463 val
->add_array_entry(s
);
465 if (!this->skip_whitespace(false))
467 if (*this->p_
== ']')
472 if (*this->p_
!= ',')
474 this->error("expected %<,%> or %<]%>");
481 // Read a JSON value into VAL. Return whether it succeeded.
484 Embedcfg_reader::read_value(Json_value
* val
)
486 if (!this->skip_whitespace(false))
493 if (!this->read_string(&s
))
500 return this->read_object(val
);
503 return this->read_array(val
);
506 this->error("invalid JSON syntax");
511 // Read a JSON string. Return whether it succeeded.
514 Embedcfg_reader::read_string(std::string
* str
)
516 if (!this->skip_whitespace(false))
518 if (*this->p_
!= '"')
520 this->error("expected %<\"%>");
526 while (this->p_
< this->pend_
&& *this->p_
!= '"')
528 if (*this->p_
!= '\\')
530 str
->push_back(*this->p_
);
536 if (this->p_
>= this->pend_
)
538 this->error("unterminated string");
543 case '"': case '\\': case '/':
544 str
->push_back(*this->p_
);
549 str
->push_back('\b');
554 str
->push_back('\f');
559 str
->push_back('\n');
564 str
->push_back('\r');
569 str
->push_back('\t');
576 unsigned int rune
= 0;
577 for (int i
= 0; i
< 4; i
++)
579 if (this->p_
>= this->pend_
)
581 this->error("unterminated string");
584 unsigned char c
= *this->p_
;
587 if (c
>= '0' && c
<= '9')
589 else if (c
>= 'A' && c
<= 'F')
590 rune
+= c
- 'A' + 10;
591 else if (c
>= 'a' && c
<= 'f')
592 rune
+= c
- 'a' + 10;
595 this->error("invalid hex digit");
599 Lex::append_char(rune
, false, str
, Linemap::unknown_location());
604 this->error("unrecognized string escape");
609 if (*this->p_
== '"')
615 this->error("unterminated string");
619 // Report an error if not at EOF.
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.
631 Embedcfg_reader::skip_whitespace(bool eof_ok
)
633 while (this->p_
< this->pend_
)
637 case ' ': case '\t': case '\n': case '\r':
645 this->error("unexpected EOF");
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",
658 static_cast<unsigned long>(this->p_
- this->data_
.data()),
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.
673 operator()(const std::string
& p1
, const std::string
& p2
) const;
677 split(const std::string
&, size_t*, size_t*, size_t*) const;
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
);
692 int i
= p2
.compare(0, dirlen2
, ".");
697 else if (dirlen2
== 0)
699 int i
= p1
.compare(0, dirlen1
, ".");
705 int i
= p1
.compare(0, dirlen1
, p2
, 0, dirlen2
);
710 int i
= p1
.compare(elem1
, elemlen1
, p2
, elem2
, elemlen2
);
714 // Pick out the directory and file name components for comparison.
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] == '/')
723 size_t slash
= s
.rfind('/', len
- 1);
724 if (slash
== std::string::npos
)
734 *elemlen
= len
- (slash
+ 1);
738 // Convert the go:embed directives for a variable into an initializer
739 // for that variable.
742 Gogo::initializer_for_embeds(Type
* type
,
743 const std::vector
<std::string
>* embeds
,
746 if (this->embed_patterns_
.empty())
749 ("invalid go:embed: build system did not "
750 "supply embed configuration"));
751 return Expression::make_error(loc
);
754 type
= type
->unalias();
762 const Named_type
* nt
= type
->named_type();
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
;
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();
789 Embed_patterns::const_iterator pp
= this->embed_patterns_
.find(*pe
);
790 if (pp
== this->embed_patterns_
.end())
793 ("invalid go:embed: build system did not "
794 "map pattern %<%s%>"),
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();
805 if (this->embed_files_
.find(*pf
) == this->embed_files_
.end())
808 ("invalid go:embed: build system did not "
814 std::pair
<Unordered_set(std::string
)::iterator
, bool> ins
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
;
827 i
= path
.rfind('/', i
);
828 if (i
== std::string::npos
)
830 std::string dir
= path
.substr(0, i
+ 1);
831 ins
= have
.insert(dir
);
833 paths
.push_back(dir
);
841 if (embed_kind
== EMBED_STRING
|| embed_kind
== EMBED_BYTES
)
843 if (paths
.size() > 1)
846 ("invalid go:embed: multiple files for "
847 "string or byte slice"));;
848 return Expression::make_error(loc
);
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
);
861 std::sort(paths
.begin(), paths
.end(), Embedfs_sort());
863 if (type
->struct_type() == NULL
864 || type
->struct_type()->field_count() != 1)
867 ("internal error: embed.FS should be struct type "
869 return Expression::make_error(loc
);
872 Type
* ptr_type
= type
->struct_type()->field(0)->type();
873 if (ptr_type
->points_to() == NULL
)
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())
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
)
893 || (file_type
->struct_type()->find_local_field(".embed.data", NULL
)
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();
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();
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
));
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
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
);