2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Parser/parsing.cpp --------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Parser/parsing.h"
|
2018-03-01 08:56:10 +08:00
|
|
|
#include "preprocessor.h"
|
|
|
|
#include "prescan.h"
|
2019-12-10 03:09:44 +08:00
|
|
|
#include "type-parsers.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Parser/message.h"
|
|
|
|
#include "flang/Parser/provenance.h"
|
|
|
|
#include "flang/Parser/source.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2018-05-03 04:48:12 +08:00
|
|
|
namespace Fortran::parser {
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2018-07-28 05:58:14 +08:00
|
|
|
Parsing::Parsing(AllSources &s) : cooked_{s} {}
|
|
|
|
Parsing::~Parsing() {}
|
|
|
|
|
2019-09-24 08:10:58 +08:00
|
|
|
const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
|
2018-03-01 08:56:10 +08:00
|
|
|
options_ = options;
|
2019-09-24 08:10:58 +08:00
|
|
|
AllSources &allSources{cooked_.allSources()};
|
|
|
|
if (options.isModuleFile) {
|
|
|
|
for (const auto &path : options.searchDirectories) {
|
|
|
|
allSources.PushSearchPathDirectory(path);
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream fileError{buf};
|
2018-04-07 01:34:59 +08:00
|
|
|
const SourceFile *sourceFile;
|
|
|
|
if (path == "-") {
|
2020-02-28 23:11:03 +08:00
|
|
|
sourceFile = allSources.ReadStandardInput(fileError);
|
2018-04-07 01:34:59 +08:00
|
|
|
} else {
|
2020-02-28 23:11:03 +08:00
|
|
|
sourceFile = allSources.Open(path, fileError);
|
2018-04-07 01:34:59 +08:00
|
|
|
}
|
2019-09-24 08:10:58 +08:00
|
|
|
if (!fileError.str().empty()) {
|
2018-07-28 02:44:31 +08:00
|
|
|
ProvenanceRange range{allSources.AddCompilerInsertion(path)};
|
2019-05-07 00:33:45 +08:00
|
|
|
messages_.Say(range, "%s"_err_en_US, fileError.str());
|
2019-09-24 08:10:58 +08:00
|
|
|
return sourceFile;
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
2019-12-04 01:49:44 +08:00
|
|
|
CHECK(sourceFile);
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2019-09-24 08:10:58 +08:00
|
|
|
if (!options.isModuleFile) {
|
|
|
|
// For .mod files we always want to look in the search directories.
|
|
|
|
// For normal source files we don't push them until after the primary
|
|
|
|
// source file has been opened. If foo.f is missing from the current
|
|
|
|
// working directory, we don't want to accidentally read another foo.f
|
|
|
|
// from another directory that's on the search path.
|
|
|
|
for (const auto &path : options.searchDirectories) {
|
|
|
|
allSources.PushSearchPathDirectory(path);
|
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
}
|
|
|
|
|
2018-07-28 02:44:31 +08:00
|
|
|
Preprocessor preprocessor{allSources};
|
2018-03-17 07:13:49 +08:00
|
|
|
for (const auto &predef : options.predefinitions) {
|
2019-11-10 01:29:31 +08:00
|
|
|
if (predef.second) {
|
2018-03-17 07:13:49 +08:00
|
|
|
preprocessor.Define(predef.first, *predef.second);
|
|
|
|
} else {
|
|
|
|
preprocessor.Undefine(predef.first);
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 05:33:41 +08:00
|
|
|
Prescanner prescanner{messages_, cooked_, preprocessor, options.features};
|
2018-03-01 08:56:10 +08:00
|
|
|
prescanner.set_fixedForm(options.isFixedForm)
|
|
|
|
.set_fixedFormColumnLimit(options.fixedFormColumns)
|
2018-03-24 06:14:52 +08:00
|
|
|
.AddCompilerDirectiveSentinel("dir$");
|
[flang][openacc] OpenACC 3.0 parser
Summary:
This patch introduce the parser for OpenACC 3.0 in Flang. It uses the same TableGen mechanism
than OpenMP.
Reviewers: nvdatian, sscalpone, tskeith, klausler, ichoyjx, jdoerfert, DavidTruby
Reviewed By: klausler
Subscribers: MaskRay, SouraVX, mgorny, hiraditya, jfb, sstefan1, llvm-commits
Tags: #llvm, #flang
Differential Revision: https://reviews.llvm.org/D83649
2020-07-15 02:28:34 +08:00
|
|
|
if (options.features.IsEnabled(LanguageFeature::OpenACC)) {
|
|
|
|
prescanner.AddCompilerDirectiveSentinel("$acc");
|
|
|
|
}
|
2018-07-19 02:19:21 +08:00
|
|
|
if (options.features.IsEnabled(LanguageFeature::OpenMP)) {
|
2018-05-16 22:28:54 +08:00
|
|
|
prescanner.AddCompilerDirectiveSentinel("$omp");
|
2020-03-29 12:00:16 +08:00
|
|
|
prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line
|
2018-05-16 22:28:54 +08:00
|
|
|
}
|
2018-07-28 06:18:36 +08:00
|
|
|
ProvenanceRange range{allSources.AddIncludedFile(
|
|
|
|
*sourceFile, ProvenanceRange{}, options.isModuleFile)};
|
2018-04-03 06:51:04 +08:00
|
|
|
prescanner.Prescan(range);
|
2019-12-07 01:37:07 +08:00
|
|
|
if (cooked_.BufferedBytes() == 0 && !options.isModuleFile) {
|
|
|
|
// Input is empty. Append a newline so that any warning
|
|
|
|
// message about nonstandard usage will have provenance.
|
|
|
|
cooked_.Put('\n', range.start());
|
|
|
|
}
|
2018-07-27 05:33:41 +08:00
|
|
|
cooked_.Marshal();
|
2019-09-07 04:00:47 +08:00
|
|
|
if (options.needProvenanceRangeToCharBlockMappings) {
|
|
|
|
cooked_.CompileProvenanceRangeToOffsetMappings();
|
|
|
|
}
|
2019-09-24 08:10:58 +08:00
|
|
|
return sourceFile;
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void Parsing::DumpCookedChars(llvm::raw_ostream &out) const {
|
2019-11-07 03:15:03 +08:00
|
|
|
UserState userState{cooked_, common::LanguageFeatureControl{}};
|
2018-07-27 05:33:41 +08:00
|
|
|
ParseState parseState{cooked_};
|
2018-03-01 08:56:10 +08:00
|
|
|
parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
|
2018-04-03 07:33:10 +08:00
|
|
|
while (std::optional<const char *> p{parseState.GetNextChar()}) {
|
|
|
|
out << **p;
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void Parsing::DumpProvenance(llvm::raw_ostream &out) const {
|
|
|
|
cooked_.Dump(out);
|
|
|
|
}
|
2018-03-01 08:56:10 +08:00
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void Parsing::DumpParsingLog(llvm::raw_ostream &out) const {
|
2018-07-27 05:33:41 +08:00
|
|
|
log_.Dump(out, cooked_);
|
2018-04-20 06:46:02 +08:00
|
|
|
}
|
2018-04-20 04:03:23 +08:00
|
|
|
|
2020-02-28 23:11:03 +08:00
|
|
|
void Parsing::Parse(llvm::raw_ostream &out) {
|
2018-07-27 05:33:41 +08:00
|
|
|
UserState userState{cooked_, options_.features};
|
2018-04-21 07:14:57 +08:00
|
|
|
userState.set_debugOutput(out)
|
|
|
|
.set_instrumentedParse(options_.instrumentedParse)
|
|
|
|
.set_log(&log_);
|
2018-07-27 05:33:41 +08:00
|
|
|
ParseState parseState{cooked_};
|
2019-06-13 06:26:37 +08:00
|
|
|
parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
|
2018-04-21 02:23:51 +08:00
|
|
|
parseTree_ = program.Parse(parseState);
|
2018-04-03 06:51:04 +08:00
|
|
|
CHECK(
|
2018-04-14 07:00:03 +08:00
|
|
|
!parseState.anyErrorRecovery() || parseState.messages().AnyFatalError());
|
2018-03-01 08:56:10 +08:00
|
|
|
consumedWholeFile_ = parseState.IsAtEnd();
|
2018-08-09 02:29:05 +08:00
|
|
|
messages_.Annex(std::move(parseState.messages()));
|
2018-04-18 06:47:51 +08:00
|
|
|
finalRestingPlace_ = parseState.GetLocation();
|
2018-04-03 06:51:04 +08:00
|
|
|
}
|
|
|
|
|
2018-04-20 08:02:12 +08:00
|
|
|
void Parsing::ClearLog() { log_.clear(); }
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::parser
|