llvm/flang/lib/parser/indirection.h
peter klausler 79d044e9b5 [flang] Take flang-compiler/f18#2 on unparsing, now using the new parse tree walker.
Clean out old data structure formatter.

Create stand-alone Parsing class to compose parts of the parser together.

Hello, world!

Better error recovery on command line errors.

Fix bugs from initial run at f77_correct.

Allow parentheses on PROGRAM statement.

Fix Hollerith scanning.

Remove REDIMENSION with good error recovery.

Fix new "spaces" parser, clean up calls to it.

Fix bugs exposed by in38.f90.

Escaped \a is not special to pgf90; get slashes around STRUCTURE name right.

Better multi-byte source encoding support in Hollerith.

Reformat C++.

More work on multi-byte source encoding.

Pass 219 tests in f77_correct, with good excuses for the rest.

Original-commit: flang-compiler/f18@8a1a0aa2dc
Reviewed-on: https://github.com/flang-compiler/f18/pull/25
Tree-same-pre-rewrite: false
2018-03-13 16:32:09 -07:00

55 lines
1.5 KiB
C++

#ifndef FORTRAN_PARSER_INDIRECTION_H_
#define FORTRAN_PARSER_INDIRECTION_H_
// Defines a smart pointer template class that's rather like std::unique_ptr<>
// but further restricted, like a C++ reference, to be non-null when constructed
// or assigned. Users need not check whether these pointers are null.
// Intended to be as invisible as possible.
#include "idioms.h"
#include <utility>
namespace Fortran {
namespace parser {
template<typename A> class Indirection {
public:
using element_type = A;
Indirection() = delete;
Indirection(A *&p) : p_{p} {
CHECK(p_ && "assigning null pointer to Indirection");
p = nullptr;
}
Indirection(A *&&p) : p_{p} {
CHECK(p_ && "assigning null pointer to Indirection");
p = nullptr;
}
Indirection(A &&p) : p_{new A(std::move(p))} {}
template<typename... ARGS>
Indirection(ARGS &&... args) : p_{new A(std::forward<ARGS>(args)...)} {}
Indirection(Indirection &&that) {
CHECK(that.p_ && "constructing Indirection from null Indirection");
p_ = that.p_;
that.p_ = nullptr;
}
~Indirection() {
delete p_;
p_ = nullptr;
}
Indirection &operator=(Indirection &&that) {
CHECK(that.p_ && "assigning null Indirection to Indirection");
auto tmp = p_;
p_ = that.p_;
that.p_ = tmp;
return *this;
}
A &operator*() const { return *p_; }
A *operator->() { return p_; }
const A *operator->() const { return p_; }
private:
A *p_{nullptr};
};
} // namespace parser
} // namespace Fortran
#endif // FORTRAN_PARSER_INDIRECTION_H_