aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorCohenArthur <arthur.cohen@epita.fr>2021-08-07 13:15:18 +0200
committerCohenArthur <arthur.cohen@epita.fr>2021-08-12 16:52:22 +0200
commit53756e1a01c7ac93e8467f577f50c491ca4b2525 (patch)
tree6258a77e9c1b17f8bc7a3a8705a1918ac2850ce8 /gcc
parent7be0232c686a75f98b2ca3c27f7de3139b8999c6 (diff)
downloadgcc-53756e1a01c7ac93e8467f577f50c491ca4b2525.zip
gcc-53756e1a01c7ac93e8467f577f50c491ca4b2525.tar.gz
gcc-53756e1a01c7ac93e8467f577f50c491ca4b2525.tar.bz2
lexer: Add function to get original filename
When parsing an external module, it is important to be able to access the original "including" file in order to resolve the path of the desired module. Take the following example ```rust // /project/src/foo.rs mod bar; /* External module */ ``` The file which contains the `bar` module could be located at `/project/src/bar.rs`, or at `/project/src/foo/bar.rs` (careful, this isn't an exhaustive list of all possible cases). We must therefore be able to access the `/project/src` directory in order to find valid candidates for `mod bar`
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-item.h10
-rw-r--r--gcc/rust/lex/rust-lex.h5
-rw-r--r--gcc/rust/parse/rust-parse-impl.h4
3 files changed, 13 insertions, 6 deletions
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 32fb56f..9e57f5a 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -977,6 +977,8 @@ private:
Location locus;
ModuleKind kind;
+ // Name of the file including the module
+ std::string outer_filename;
// bool has_inner_attrs;
std::vector<Attribute> inner_attrs;
// bool has_items;
@@ -998,10 +1000,11 @@ public:
// Unloaded module constructor
Module (Identifier module_name, Visibility visibility,
- std::vector<Attribute> outer_attrs, Location locus)
+ std::vector<Attribute> outer_attrs, Location locus,
+ std::string outer_filename)
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
- inner_attrs (std::vector<Attribute> ()),
+ outer_filename (outer_filename), inner_attrs (std::vector<Attribute> ()),
items (std::vector<std::unique_ptr<Item>> ())
{}
@@ -1013,7 +1016,8 @@ public:
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (name), locus (locus), kind (ModuleKind::LOADED),
- inner_attrs (std::move (inner_attrs)), items (std::move (items))
+ outer_filename (std::string ()), inner_attrs (std::move (inner_attrs)),
+ items (std::move (items))
{}
// Copy constructor with vector clone
diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h
index 902745f..1b37a9c 100644
--- a/gcc/rust/lex/rust-lex.h
+++ b/gcc/rust/lex/rust-lex.h
@@ -14,6 +14,7 @@ struct RAIIFile
{
private:
FILE *file;
+ const char *filename;
void close ()
{
@@ -22,7 +23,7 @@ private:
}
public:
- RAIIFile (const char *filename)
+ RAIIFile (const char *filename) : filename (filename)
{
if (strcmp (filename, "-") == 0)
file = stdin;
@@ -47,6 +48,7 @@ public:
~RAIIFile () { close (); }
FILE *get_raw () { return file; }
+ const char *get_filename () { return filename; }
};
class Lexer
@@ -136,6 +138,7 @@ public:
void split_current_token (TokenId new_left, TokenId new_right);
Linemap *get_line_map () { return line_map; }
+ std::string get_filename () { return std::string (input.get_filename ()); }
private:
// File for use as input.
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index c94c637..0e50168 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -2103,8 +2103,8 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
return std::unique_ptr<AST::Module> (
new AST::Module (std::move (name), std::move (vis),
- std::move (outer_attrs),
- locus)); // module name?
+ std::move (outer_attrs), locus,
+ lexer.get_filename ()));
case LEFT_CURLY: {
lexer.skip_token ();