#include "rust-system.h"
#include "rust-diagnostics.h"
#include "rust-imports.h"
+#include "rust-make-unique.h"
#ifndef O_BINARY
#define O_BINARY 0
Stream_concatenate () : inputs_ () {}
// Add a new stream.
- void add (Import::Stream *is) { this->inputs_.push_back (is); }
+ void add (std::unique_ptr<Import::Stream> is)
+ {
+ this->inputs_.push_back (std::move (is));
+ }
protected:
bool do_peek (size_t, const char **);
void do_advance (size_t);
private:
- std::list<Import::Stream *> inputs_;
+ std::list<std::unique_ptr<Import::Stream>> inputs_;
};
// Peek ahead.
return false;
if (this->inputs_.front ()->peek (length, bytes))
return true;
- delete this->inputs_.front ();
this->inputs_.pop_front ();
}
}
this->inputs_.front ()->advance (skip);
return;
}
- delete this->inputs_.front ();
this->inputs_.pop_front ();
}
}
// Import data from an archive. We walk through the archive and
// import data from each member.
-Import::Stream *
+std::unique_ptr<Import::Stream>
Import::find_archive_export_data (const std::string &filename, int fd,
Location location)
{
Archive_file afile (filename, fd, location);
if (!afile.initialize ())
- return NULL;
+ return nullptr;
- Stream_concatenate *ret = new Stream_concatenate;
+ auto ret = Rust::make_unique<Stream_concatenate> ();
bool any_data = false;
bool any_members = false;
std::string member_name;
if (!afile.get_file_and_offset (p->off, p->name, p->nested_off,
&member_fd, &member_off, &member_name))
- return NULL;
+ return nullptr;
- Import::Stream *is
+ std::unique_ptr<Import::Stream> is
= Import::find_object_export_data (member_name, member_fd, member_off,
location);
- if (is != NULL)
+ if (is != nullptr)
{
- ret->add (is);
+ ret->add (std::move (is));
any_data = true;
}
}
if (!any_members)
{
// It's normal to have an empty archive file when using gobuild.
- return new Stream_from_string ("");
+ return Rust::make_unique<Stream_from_string> ("");
}
if (!any_data)
{
- delete ret;
- return NULL;
+ return nullptr;
}
- return ret;
+ return std::unique_ptr<Stream>{static_cast<Stream *> (ret.release ())};
}
} // namespace Rust
#include "rust-imports.h"
#include "rust-object-export.h"
#include "rust-export-metadata.h"
+#include "rust-make-unique.h"
#ifndef O_BINARY
#define O_BINARY 0
// stop; we do not keep looking for another file with the same name
// later in the search path.
-std::pair<Import::Stream *, std::vector<ProcMacro::Procmacro>>
+std::pair<std::unique_ptr<Import::Stream>, std::vector<ProcMacro::Procmacro>>
Import::open_package (const std::string &filename, Location location,
const std::string &relative_import_path)
{
indir += '/';
indir += fn;
auto s = Import::try_package_in_directory (indir, location);
- if (s.first != NULL)
+ if (s.first != nullptr)
return s;
}
}
auto s = Import::try_package_in_directory (fn, location);
- if (s.first != NULL)
+ if (s.first != nullptr)
return s;
- return std::make_pair (NULL, std::vector<ProcMacro::Procmacro>{});
+ return std::make_pair (nullptr, std::vector<ProcMacro::Procmacro>{});
}
// Try to find the export data for FILENAME.
-std::pair<Import::Stream *, std::vector<ProcMacro::Procmacro>>
+std::pair<std::unique_ptr<Import::Stream>, std::vector<ProcMacro::Procmacro>>
Import::try_package_in_directory (const std::string &filename,
Location location)
{
fd = Import::try_suffixes (&found_filename);
if (fd < 0)
- return std::make_pair (NULL, std::vector<ProcMacro::Procmacro>{});
+ return std::make_pair (nullptr, std::vector<ProcMacro::Procmacro>{});
}
// The export data may not be in this file.
- Stream *s = Import::find_export_data (found_filename, fd, location);
- if (s != NULL)
- return std::make_pair (s, std::vector<ProcMacro::Procmacro>{});
+ std::unique_ptr<Stream> s
+ = Import::find_export_data (found_filename, fd, location);
+ if (s != nullptr)
+ return std::make_pair (std::move (s), std::vector<ProcMacro::Procmacro>{});
close (fd);
// Look for export data in the file descriptor FD.
-Import::Stream *
+std::unique_ptr<Import::Stream>
Import::find_export_data (const std::string &filename, int fd,
Location location)
{
// See if we can read this as an object file.
- Import::Stream *stream
+ std::unique_ptr<Import::Stream> stream
= Import::find_object_export_data (filename, fd, 0, location);
- if (stream != NULL)
+ if (stream != nullptr)
return stream;
const int len = sizeof (Metadata::kMagicHeader);
if (lseek (fd, 0, SEEK_SET) < 0)
{
rust_error_at (location, "lseek %s failed: %m", filename.c_str ());
- return NULL;
+ return nullptr;
}
char buf[len];
ssize_t c = ::read (fd, buf, len);
if (c < len)
- return NULL;
+ return nullptr;
// Check for a file containing nothing but Rust export data.
// if (memcmp (buf, Export::cur_magic, Export::magic_len) == 0
//
if (memcmp (buf, Metadata::kMagicHeader, sizeof (Metadata::kMagicHeader))
== 0)
- return new Stream_from_file (fd);
+ return Rust::make_unique<Stream_from_file> (fd);
// See if we can read this as an archive.
if (Import::is_archive_magic (buf))
return Import::find_archive_export_data (filename, fd, location);
- return NULL;
+ return nullptr;
}
// Look for export data in an object file.
-Import::Stream *
+std::unique_ptr<Import::Stream>
Import::find_object_export_data (const std::string &filename, int fd,
off_t offset, Location location)
{
size_t len;
int err;
const char *errmsg = rust_read_export_data (fd, offset, &buf, &len, &err);
- if (errmsg != NULL)
+ if (errmsg != nullptr)
{
if (err == 0)
rust_error_at (location, "%s: %s", filename.c_str (), errmsg);
else
rust_error_at (location, "%s: %s: %s", filename.c_str (), errmsg,
xstrerror (err));
- return NULL;
+ return nullptr;
}
- if (buf == NULL)
- return NULL;
+ if (buf == nullptr)
+ return nullptr;
- return new Stream_from_buffer (buf, len);
+ return Rust::make_unique<Stream_from_buffer> (buf, len);
}
// Class Import.
// Construct an Import object. We make the builtin_types_ vector
// large enough to hold all the builtin types.
-Import::Import (Stream *stream, Location location)
- : stream_ (stream), location_ (location)
+Import::Import (std::unique_ptr<Stream> stream, Location location)
+ : stream_ (std::move (stream)), location_ (location)
{}
// Import the data in the associated stream.
// returns a pointer to a Stream object to read the data that it
// exports. LOCATION is the location of the import statement.
// RELATIVE_IMPORT_PATH is used as a prefix for a relative import.
- static std::pair<Stream *, std::vector<ProcMacro::Procmacro>>
+ static std::pair<std::unique_ptr<Stream>, std::vector<ProcMacro::Procmacro>>
open_package (const std::string &filename, Location location,
const std::string &relative_import_path);
- static std::pair<Stream *, std::vector<ProcMacro::Procmacro>>
+ static std::pair<std::unique_ptr<Stream>, std::vector<ProcMacro::Procmacro>>
try_package_in_directory (const std::string &, Location);
// Constructor.
- Import (Stream *, Location);
+ Import (std::unique_ptr<Stream>, Location);
// The location of the import statement.
Location location () const { return this->location_; }
private:
static int try_suffixes (std::string *);
- static Stream *find_export_data (const std::string &filename, int fd,
- Location);
+ static std::unique_ptr<Stream> find_export_data (const std::string &filename,
+ int fd, Location);
- static Stream *find_object_export_data (const std::string &filename, int fd,
- off_t offset, Location);
+ static std::unique_ptr<Stream>
+ find_object_export_data (const std::string &filename, int fd, off_t offset,
+ Location);
static bool is_archive_magic (const char *);
- static Stream *find_archive_export_data (const std::string &filename, int fd,
- Location);
+ static std::unique_ptr<Stream>
+ find_archive_export_data (const std::string &filename, int fd, Location);
// The stream from which to read import data.
- Stream *stream_;
+ std::unique_ptr<Stream> stream_;
// The location of the import statement we are processing.
Location location_;
};