2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Parser/preprocessor.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
|
|
|
|
2018-02-08 08:27:36 +08:00
|
|
|
#include "preprocessor.h"
|
2018-01-31 03:54:31 +08:00
|
|
|
#include "prescan.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Common/idioms.h"
|
|
|
|
#include "flang/Parser/characters.h"
|
|
|
|
#include "flang/Parser/message.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-02-02 04:08:02 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cinttypes>
|
2018-03-21 01:59:07 +08:00
|
|
|
#include <cstddef>
|
2018-02-02 07:01:23 +08:00
|
|
|
#include <ctime>
|
2018-01-31 03:54:31 +08:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2018-03-20 02:48:49 +08:00
|
|
|
#include <optional>
|
2018-01-31 03:54:31 +08:00
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-05-03 04:48:12 +08:00
|
|
|
namespace Fortran::parser {
|
2018-01-31 03:54:31 +08:00
|
|
|
|
2018-02-06 06:29:26 +08:00
|
|
|
Definition::Definition(
|
2018-03-21 01:59:07 +08:00
|
|
|
const TokenSequence &repl, std::size_t firstToken, std::size_t tokens)
|
2020-03-29 12:00:16 +08:00
|
|
|
: replacement_{Tokenize({}, repl, firstToken, tokens)} {}
|
2018-01-31 03:54:31 +08:00
|
|
|
|
|
|
|
Definition::Definition(const std::vector<std::string> &argNames,
|
2018-03-21 01:59:07 +08:00
|
|
|
const TokenSequence &repl, std::size_t firstToken, std::size_t tokens,
|
2018-02-06 06:29:26 +08:00
|
|
|
bool isVariadic)
|
2020-03-29 12:00:16 +08:00
|
|
|
: isFunctionLike_{true},
|
|
|
|
argumentCount_(argNames.size()), isVariadic_{isVariadic},
|
|
|
|
replacement_{Tokenize(argNames, repl, firstToken, tokens)} {}
|
2018-01-31 03:54:31 +08:00
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
Definition::Definition(const std::string &predefined, AllSources &sources)
|
2020-03-29 12:00:16 +08:00
|
|
|
: isPredefined_{true},
|
|
|
|
replacement_{
|
|
|
|
predefined, sources.AddCompilerInsertion(predefined).start()} {}
|
2018-02-02 07:01:23 +08:00
|
|
|
|
2018-01-31 03:54:31 +08:00
|
|
|
bool Definition::set_isDisabled(bool disable) {
|
|
|
|
bool was{isDisabled_};
|
|
|
|
isDisabled_ = disable;
|
|
|
|
return was;
|
|
|
|
}
|
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
static bool IsLegalIdentifierStart(const CharBlock &cpl) {
|
2018-02-28 06:02:10 +08:00
|
|
|
return cpl.size() > 0 && IsLegalIdentifierStart(cpl[0]);
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 03:54:31 +08:00
|
|
|
TokenSequence Definition::Tokenize(const std::vector<std::string> &argNames,
|
2018-03-21 01:59:07 +08:00
|
|
|
const TokenSequence &token, std::size_t firstToken, std::size_t tokens) {
|
2018-01-31 03:54:31 +08:00
|
|
|
std::map<std::string, std::string> args;
|
|
|
|
char argIndex{'A'};
|
|
|
|
for (const std::string &arg : argNames) {
|
|
|
|
CHECK(args.find(arg) == args.end());
|
|
|
|
args[arg] = "~"s + argIndex++;
|
|
|
|
}
|
|
|
|
TokenSequence result;
|
2018-03-21 01:59:07 +08:00
|
|
|
for (std::size_t j{0}; j < tokens; ++j) {
|
2018-03-24 06:14:52 +08:00
|
|
|
CharBlock tok{token.TokenAt(firstToken + j)};
|
2018-02-28 06:02:10 +08:00
|
|
|
if (IsLegalIdentifierStart(tok)) {
|
2018-07-11 08:09:07 +08:00
|
|
|
auto it{args.find(tok.ToString())};
|
2018-01-31 07:22:26 +08:00
|
|
|
if (it != args.end()) {
|
2018-02-16 05:13:28 +08:00
|
|
|
result.Put(it->second, token.GetTokenProvenance(j));
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 05:26:32 +08:00
|
|
|
result.Put(token, firstToken + j, 1);
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-17 03:14:11 +08:00
|
|
|
static TokenSequence Stringify(
|
2018-03-24 06:14:52 +08:00
|
|
|
const TokenSequence &tokens, AllSources &allSources) {
|
2018-02-17 03:14:11 +08:00
|
|
|
TokenSequence result;
|
2018-03-24 06:14:52 +08:00
|
|
|
Provenance quoteProvenance{allSources.CompilerInsertionProvenance('"')};
|
2018-02-17 03:14:11 +08:00
|
|
|
result.PutNextTokenChar('"', quoteProvenance);
|
2018-03-24 06:14:52 +08:00
|
|
|
for (std::size_t j{0}; j < tokens.SizeInTokens(); ++j) {
|
|
|
|
const CharBlock &token{tokens.TokenAt(j)};
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t bytes{token.size()};
|
|
|
|
for (std::size_t k{0}; k < bytes; ++k) {
|
2018-02-17 03:14:11 +08:00
|
|
|
char ch{token[k]};
|
|
|
|
Provenance from{tokens.GetTokenProvenance(j, k)};
|
|
|
|
if (ch == '"' || ch == '\\') {
|
|
|
|
result.PutNextTokenChar(ch, from);
|
|
|
|
}
|
|
|
|
result.PutNextTokenChar(ch, from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.PutNextTokenChar('"', quoteProvenance);
|
|
|
|
result.CloseToken();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-18 03:19:42 +08:00
|
|
|
constexpr bool IsTokenPasting(CharBlock opr) {
|
|
|
|
return opr.size() == 2 && opr[0] == '#' && opr[1] == '#';
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AnyTokenPasting(const TokenSequence &text) {
|
|
|
|
std::size_t tokens{text.SizeInTokens()};
|
|
|
|
for (std::size_t j{0}; j < tokens; ++j) {
|
|
|
|
if (IsTokenPasting(text.TokenAt(j))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TokenSequence TokenPasting(TokenSequence &&text) {
|
|
|
|
if (!AnyTokenPasting(text)) {
|
|
|
|
return std::move(text);
|
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
TokenSequence result;
|
2020-09-18 03:19:42 +08:00
|
|
|
std::size_t tokens{text.SizeInTokens()};
|
2018-02-03 07:52:43 +08:00
|
|
|
bool pasting{false};
|
2020-09-18 03:19:42 +08:00
|
|
|
for (std::size_t j{0}; j < tokens; ++j) {
|
|
|
|
if (IsTokenPasting(text.TokenAt(j))) {
|
|
|
|
if (!pasting) {
|
|
|
|
while (!result.empty() &&
|
|
|
|
result.TokenAt(result.SizeInTokens() - 1).IsBlank()) {
|
|
|
|
result.pop_back();
|
|
|
|
}
|
|
|
|
if (!result.empty()) {
|
|
|
|
result.ReopenLastToken();
|
|
|
|
pasting = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (pasting && text.TokenAt(j).IsBlank()) {
|
|
|
|
} else {
|
|
|
|
result.Put(text, j, 1);
|
|
|
|
pasting = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenSequence Definition::Apply(
|
|
|
|
const std::vector<TokenSequence> &args, Prescanner &prescanner) {
|
|
|
|
TokenSequence result;
|
2018-02-06 04:48:09 +08:00
|
|
|
bool skipping{false};
|
|
|
|
int parenthesesNesting{0};
|
2018-03-24 06:14:52 +08:00
|
|
|
std::size_t tokens{replacement_.SizeInTokens()};
|
2018-03-21 01:59:07 +08:00
|
|
|
for (std::size_t j{0}; j < tokens; ++j) {
|
2020-09-18 03:19:42 +08:00
|
|
|
CharBlock token{replacement_.TokenAt(j)};
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t bytes{token.size()};
|
2018-02-06 04:48:09 +08:00
|
|
|
if (skipping) {
|
|
|
|
if (bytes == 1) {
|
|
|
|
if (token[0] == '(') {
|
2018-02-06 06:29:26 +08:00
|
|
|
++parenthesesNesting;
|
2018-02-06 04:48:09 +08:00
|
|
|
} else if (token[0] == ')') {
|
|
|
|
skipping = --parenthesesNesting > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-18 03:19:42 +08:00
|
|
|
if (bytes == 2 && token[0] == '~') { // argument substitution
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t index = token[1] - 'A';
|
2018-01-31 03:54:31 +08:00
|
|
|
if (index >= args.size()) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-18 03:19:42 +08:00
|
|
|
std::size_t prev{j};
|
|
|
|
while (prev > 0 && replacement_.TokenAt(prev - 1).IsBlank()) {
|
|
|
|
--prev;
|
|
|
|
}
|
|
|
|
if (prev > 0 && replacement_.TokenAt(prev - 1).size() == 1 &&
|
|
|
|
replacement_.TokenAt(prev - 1)[0] ==
|
|
|
|
'#') { // stringify argument without macro replacement
|
|
|
|
std::size_t resultSize{result.SizeInTokens()};
|
2021-12-30 04:14:25 +08:00
|
|
|
while (resultSize > 0 && result.TokenAt(resultSize - 1).IsBlank()) {
|
2018-02-03 07:52:43 +08:00
|
|
|
result.pop_back();
|
2021-12-30 04:14:25 +08:00
|
|
|
--resultSize;
|
2018-02-03 07:52:43 +08:00
|
|
|
}
|
2020-09-18 03:19:42 +08:00
|
|
|
CHECK(resultSize > 0 &&
|
|
|
|
result.TokenAt(resultSize - 1) == replacement_.TokenAt(prev - 1));
|
|
|
|
result.pop_back();
|
|
|
|
result.Put(Stringify(args[index], prescanner.allSources()));
|
2018-01-31 03:54:31 +08:00
|
|
|
} else {
|
2020-09-18 03:19:42 +08:00
|
|
|
const TokenSequence *arg{&args[index]};
|
|
|
|
std::optional<TokenSequence> replaced;
|
|
|
|
// Don't replace macros in the actual argument if it is preceded or
|
|
|
|
// followed by the token-pasting operator ## in the replacement text.
|
|
|
|
if (prev == 0 || !IsTokenPasting(replacement_.TokenAt(prev - 1))) {
|
|
|
|
auto next{replacement_.SkipBlanks(j + 1)};
|
|
|
|
if (next >= tokens || !IsTokenPasting(replacement_.TokenAt(next))) {
|
|
|
|
// Apply macro replacement to the actual argument
|
|
|
|
replaced =
|
|
|
|
prescanner.preprocessor().MacroReplacement(*arg, prescanner);
|
|
|
|
if (replaced) {
|
|
|
|
arg = &*replaced;
|
|
|
|
}
|
2018-01-31 07:22:26 +08:00
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2020-09-18 03:19:42 +08:00
|
|
|
result.Put(DEREF(arg));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-02-06 04:48:09 +08:00
|
|
|
} else if (bytes == 11 && isVariadic_ &&
|
2018-04-04 06:50:25 +08:00
|
|
|
token.ToString() == "__VA_ARGS__") {
|
2020-09-18 03:19:42 +08:00
|
|
|
Provenance commaProvenance{
|
|
|
|
prescanner.preprocessor().allSources().CompilerInsertionProvenance(
|
|
|
|
',')};
|
2018-03-21 01:59:07 +08:00
|
|
|
for (std::size_t k{argumentCount_}; k < args.size(); ++k) {
|
2018-02-06 04:48:09 +08:00
|
|
|
if (k > argumentCount_) {
|
2018-02-17 03:14:11 +08:00
|
|
|
result.Put(","s, commaProvenance);
|
2018-02-06 04:48:09 +08:00
|
|
|
}
|
2018-02-10 06:04:11 +08:00
|
|
|
result.Put(args[k]);
|
2018-02-06 04:48:09 +08:00
|
|
|
}
|
2018-02-06 06:29:26 +08:00
|
|
|
} else if (bytes == 10 && isVariadic_ && token.ToString() == "__VA_OPT__" &&
|
2018-03-24 06:14:52 +08:00
|
|
|
j + 2 < tokens && replacement_.TokenAt(j + 1).ToString() == "(" &&
|
2018-02-06 06:29:26 +08:00
|
|
|
parenthesesNesting == 0) {
|
2018-02-06 04:48:09 +08:00
|
|
|
parenthesesNesting = 1;
|
|
|
|
skipping = args.size() == argumentCount_;
|
|
|
|
++j;
|
2018-01-31 03:54:31 +08:00
|
|
|
} else {
|
2018-02-06 04:48:09 +08:00
|
|
|
if (bytes == 1 && parenthesesNesting > 0 && token[0] == '(') {
|
|
|
|
++parenthesesNesting;
|
|
|
|
} else if (bytes == 1 && parenthesesNesting > 0 && token[0] == ')') {
|
|
|
|
if (--parenthesesNesting == 0) {
|
|
|
|
skipping = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 06:04:11 +08:00
|
|
|
result.Put(replacement_, j);
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 03:19:42 +08:00
|
|
|
return TokenPasting(std::move(result));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:01:23 +08:00
|
|
|
static std::string FormatTime(const std::time_t &now, const char *format) {
|
|
|
|
char buffer[16];
|
|
|
|
return {buffer,
|
2018-02-06 06:29:26 +08:00
|
|
|
std::strftime(buffer, sizeof buffer, format, std::localtime(&now))};
|
2018-02-02 07:01:23 +08:00
|
|
|
}
|
|
|
|
|
2021-02-11 08:15:20 +08:00
|
|
|
Preprocessor::Preprocessor(AllSources &allSources) : allSources_{allSources} {}
|
|
|
|
|
|
|
|
void Preprocessor::DefineStandardMacros() {
|
2018-02-02 07:01:23 +08:00
|
|
|
// Capture current local date & time once now to avoid having the values
|
|
|
|
// of __DATE__ or __TIME__ change during compilation.
|
|
|
|
std::time_t now;
|
|
|
|
std::time(&now);
|
2021-02-11 08:15:20 +08:00
|
|
|
Define("__DATE__"s, FormatTime(now, "\"%h %e %Y\"")); // e.g., "Jun 16 1904"
|
|
|
|
Define("__TIME__"s, FormatTime(now, "\"%T\"")); // e.g., "23:59:60"
|
2018-02-02 07:01:23 +08:00
|
|
|
// The values of these predefined macros depend on their invocation sites.
|
2021-02-11 08:15:20 +08:00
|
|
|
Define("__FILE__"s, "__FILE__"s);
|
|
|
|
Define("__LINE__"s, "__LINE__"s);
|
2018-02-02 07:01:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 07:13:49 +08:00
|
|
|
void Preprocessor::Define(std::string macro, std::string value) {
|
2020-08-13 07:20:20 +08:00
|
|
|
definitions_.emplace(SaveTokenAsName(macro), Definition{value, allSources_});
|
2018-03-17 07:13:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::Undefine(std::string macro) { definitions_.erase(macro); }
|
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
std::optional<TokenSequence> Preprocessor::MacroReplacement(
|
2020-09-18 03:19:42 +08:00
|
|
|
const TokenSequence &input, Prescanner &prescanner) {
|
2018-01-31 03:54:31 +08:00
|
|
|
// Do quick scan for any use of a defined name.
|
2021-02-11 08:15:20 +08:00
|
|
|
if (definitions_.empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
std::size_t tokens{input.SizeInTokens()};
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t j;
|
2018-01-31 03:54:31 +08:00
|
|
|
for (j = 0; j < tokens; ++j) {
|
2018-03-24 06:14:52 +08:00
|
|
|
CharBlock token{input.TokenAt(j)};
|
|
|
|
if (!token.empty() && IsLegalIdentifierStart(token[0]) &&
|
|
|
|
IsNameDefined(token)) {
|
2018-01-31 03:54:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == tokens) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return std::nullopt; // input contains nothing that would be replaced
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
TokenSequence result{input, 0, j};
|
2018-01-31 03:54:31 +08:00
|
|
|
for (; j < tokens; ++j) {
|
2018-03-24 06:14:52 +08:00
|
|
|
const CharBlock &token{input.TokenAt(j)};
|
2018-02-28 06:02:10 +08:00
|
|
|
if (token.IsBlank() || !IsLegalIdentifierStart(token[0])) {
|
2018-03-24 06:14:52 +08:00
|
|
|
result.Put(input, j);
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-11 08:09:07 +08:00
|
|
|
auto it{definitions_.find(token)};
|
2018-01-31 03:54:31 +08:00
|
|
|
if (it == definitions_.end()) {
|
2018-03-24 06:14:52 +08:00
|
|
|
result.Put(input, j);
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Definition &def{it->second};
|
|
|
|
if (def.isDisabled()) {
|
2018-03-24 06:14:52 +08:00
|
|
|
result.Put(input, j);
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!def.isFunctionLike()) {
|
2018-02-02 07:01:23 +08:00
|
|
|
if (def.isPredefined()) {
|
2018-03-24 06:14:52 +08:00
|
|
|
std::string name{def.replacement().TokenAt(0).ToString()};
|
2018-02-16 02:42:36 +08:00
|
|
|
std::string repl;
|
2018-02-02 07:01:23 +08:00
|
|
|
if (name == "__FILE__") {
|
2018-02-16 02:42:36 +08:00
|
|
|
repl = "\""s +
|
2018-03-24 06:14:52 +08:00
|
|
|
allSources_.GetPath(prescanner.GetCurrentProvenance()) + '"';
|
2018-02-16 02:42:36 +08:00
|
|
|
} else if (name == "__LINE__") {
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream ss{buf};
|
2018-03-24 06:14:52 +08:00
|
|
|
ss << allSources_.GetLineNumber(prescanner.GetCurrentProvenance());
|
2018-02-16 02:42:36 +08:00
|
|
|
repl = ss.str();
|
|
|
|
}
|
|
|
|
if (!repl.empty()) {
|
2018-03-24 06:14:52 +08:00
|
|
|
ProvenanceRange insert{allSources_.AddCompilerInsertion(repl)};
|
|
|
|
ProvenanceRange call{allSources_.AddMacroCall(
|
2018-02-16 05:13:28 +08:00
|
|
|
insert, input.GetTokenProvenanceRange(j), repl)};
|
2018-03-24 06:14:52 +08:00
|
|
|
result.Put(repl, call.start());
|
2018-02-02 07:01:23 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
def.set_isDisabled(true);
|
2020-09-18 03:19:42 +08:00
|
|
|
TokenSequence replaced{
|
|
|
|
TokenPasting(ReplaceMacros(def.replacement(), prescanner))};
|
2018-01-31 03:54:31 +08:00
|
|
|
def.set_isDisabled(false);
|
2018-02-16 07:56:50 +08:00
|
|
|
if (!replaced.empty()) {
|
|
|
|
ProvenanceRange from{def.replacement().GetProvenanceRange()};
|
|
|
|
ProvenanceRange use{input.GetTokenProvenanceRange(j)};
|
|
|
|
ProvenanceRange newRange{
|
2018-03-24 06:14:52 +08:00
|
|
|
allSources_.AddMacroCall(from, use, replaced.ToString())};
|
|
|
|
result.Put(replaced, newRange);
|
2018-02-16 07:56:50 +08:00
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Possible function-like macro call. Skip spaces and newlines to see
|
|
|
|
// whether '(' is next.
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t k{j};
|
2018-01-31 03:54:31 +08:00
|
|
|
bool leftParen{false};
|
|
|
|
while (++k < tokens) {
|
2018-03-24 06:14:52 +08:00
|
|
|
const CharBlock &lookAhead{input.TokenAt(k)};
|
2018-02-03 07:52:43 +08:00
|
|
|
if (!lookAhead.IsBlank() && lookAhead[0] != '\n') {
|
2018-02-02 04:08:02 +08:00
|
|
|
leftParen = lookAhead[0] == '(' && lookAhead.size() == 1;
|
2018-01-31 03:54:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!leftParen) {
|
2018-03-24 06:14:52 +08:00
|
|
|
result.Put(input, j);
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-21 01:59:07 +08:00
|
|
|
std::vector<std::size_t> argStart{++k};
|
2018-01-31 03:54:31 +08:00
|
|
|
for (int nesting{0}; k < tokens; ++k) {
|
2018-03-24 06:14:52 +08:00
|
|
|
CharBlock token{input.TokenAt(k)};
|
|
|
|
if (token.size() == 1) {
|
|
|
|
char ch{token[0]};
|
2018-02-02 04:08:02 +08:00
|
|
|
if (ch == '(') {
|
|
|
|
++nesting;
|
|
|
|
} else if (ch == ')') {
|
|
|
|
if (nesting == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--nesting;
|
|
|
|
} else if (ch == ',' && nesting == 0) {
|
|
|
|
argStart.push_back(k + 1);
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 00:33:45 +08:00
|
|
|
if (argStart.size() == 1 && k == argStart[0] && def.argumentCount() == 0) {
|
2019-05-03 06:19:37 +08:00
|
|
|
// Subtle: () is zero arguments, not one empty argument,
|
|
|
|
// unless one argument was expected.
|
2019-05-03 05:29:52 +08:00
|
|
|
argStart.clear();
|
|
|
|
}
|
2018-02-06 06:29:26 +08:00
|
|
|
if (k >= tokens || argStart.size() < def.argumentCount() ||
|
2018-02-06 04:48:09 +08:00
|
|
|
(argStart.size() > def.argumentCount() && !def.isVariadic())) {
|
2018-03-24 06:14:52 +08:00
|
|
|
result.Put(input, j);
|
2018-01-31 03:54:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::vector<TokenSequence> args;
|
2018-03-21 01:59:07 +08:00
|
|
|
for (std::size_t n{0}; n < argStart.size(); ++n) {
|
|
|
|
std::size_t at{argStart[n]};
|
|
|
|
std::size_t count{
|
|
|
|
(n + 1 == argStart.size() ? k : argStart[n + 1] - 1) - at};
|
2018-02-10 06:04:11 +08:00
|
|
|
args.emplace_back(TokenSequence(input, at, count));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
def.set_isDisabled(true);
|
2018-02-16 05:13:28 +08:00
|
|
|
TokenSequence replaced{
|
2020-09-18 03:19:42 +08:00
|
|
|
ReplaceMacros(def.Apply(args, prescanner), prescanner)};
|
2018-01-31 03:54:31 +08:00
|
|
|
def.set_isDisabled(false);
|
2018-02-16 07:56:50 +08:00
|
|
|
if (!replaced.empty()) {
|
|
|
|
ProvenanceRange from{def.replacement().GetProvenanceRange()};
|
|
|
|
ProvenanceRange use{input.GetIntervalProvenanceRange(j, k - j)};
|
|
|
|
ProvenanceRange newRange{
|
2018-03-24 06:14:52 +08:00
|
|
|
allSources_.AddMacroCall(from, use, replaced.ToString())};
|
|
|
|
result.Put(replaced, newRange);
|
2018-02-16 07:56:50 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
j = k; // advance to the terminal ')'
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2019-05-03 05:29:52 +08:00
|
|
|
return result;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
|
2018-02-14 04:24:54 +08:00
|
|
|
TokenSequence Preprocessor::ReplaceMacros(
|
2020-09-18 03:19:42 +08:00
|
|
|
const TokenSequence &tokens, Prescanner &prescanner) {
|
2018-03-24 06:14:52 +08:00
|
|
|
if (std::optional<TokenSequence> repl{MacroReplacement(tokens, prescanner)}) {
|
|
|
|
return std::move(*repl);
|
|
|
|
}
|
|
|
|
return tokens;
|
2018-02-02 07:01:23 +08:00
|
|
|
}
|
|
|
|
|
2021-09-01 08:16:01 +08:00
|
|
|
void Preprocessor::Directive(const TokenSequence &dir, Prescanner &prescanner) {
|
2018-03-24 06:14:52 +08:00
|
|
|
std::size_t tokens{dir.SizeInTokens()};
|
2018-05-12 03:54:49 +08:00
|
|
|
std::size_t j{dir.SkipBlanks(0)};
|
2018-01-31 03:54:31 +08:00
|
|
|
if (j == tokens) {
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
if (dir.TokenAt(j).ToString() != "#") {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(j), "missing '#'"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2019-03-16 06:01:54 +08:00
|
|
|
while (tokens > 0 && dir.TokenAt(tokens - 1).IsBlank()) {
|
|
|
|
--tokens;
|
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
if (j == tokens) {
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
if (IsDecimalDigit(dir.TokenAt(j)[0]) || dir.TokenAt(j)[0] == '"') {
|
2020-03-29 12:00:16 +08:00
|
|
|
return; // treat like #line, ignore it
|
2018-01-31 07:22:26 +08:00
|
|
|
}
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t dirOffset{j};
|
2018-03-24 06:14:52 +08:00
|
|
|
std::string dirName{ToLowerCaseLetters(dir.TokenAt(dirOffset).ToString())};
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2018-03-21 01:59:07 +08:00
|
|
|
CharBlock nameToken;
|
2018-03-24 06:14:52 +08:00
|
|
|
if (j < tokens && IsLegalIdentifierStart(dir.TokenAt(j)[0])) {
|
|
|
|
nameToken = dir.TokenAt(j);
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-02-02 04:08:02 +08:00
|
|
|
if (dirName == "line") {
|
2018-04-10 02:43:37 +08:00
|
|
|
// #line is ignored
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "define") {
|
2018-01-31 03:54:31 +08:00
|
|
|
if (nameToken.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(j < tokens ? j : tokens - 1),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#define: missing or invalid name"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-02-13 06:43:16 +08:00
|
|
|
nameToken = SaveTokenAsName(nameToken);
|
2018-01-31 03:54:31 +08:00
|
|
|
definitions_.erase(nameToken);
|
2018-03-24 06:14:52 +08:00
|
|
|
if (++j < tokens && dir.TokenAt(j).size() == 1 &&
|
|
|
|
dir.TokenAt(j)[0] == '(') {
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2018-01-31 03:54:31 +08:00
|
|
|
std::vector<std::string> argName;
|
2018-02-06 04:48:09 +08:00
|
|
|
bool isVariadic{false};
|
2018-03-24 06:14:52 +08:00
|
|
|
if (dir.TokenAt(j).ToString() != ")") {
|
2018-01-31 03:54:31 +08:00
|
|
|
while (true) {
|
2018-03-24 06:14:52 +08:00
|
|
|
std::string an{dir.TokenAt(j).ToString()};
|
2018-02-06 04:48:09 +08:00
|
|
|
if (an == "...") {
|
|
|
|
isVariadic = true;
|
|
|
|
} else {
|
2018-02-28 06:02:10 +08:00
|
|
|
if (an.empty() || !IsLegalIdentifierStart(an[0])) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(j),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#define: missing or invalid argument name"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-06 04:48:09 +08:00
|
|
|
}
|
|
|
|
argName.push_back(an);
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2018-01-31 03:54:31 +08:00
|
|
|
if (j == tokens) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(tokens - 1),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#define: malformed argument list"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
std::string punc{dir.TokenAt(j).ToString()};
|
2018-01-31 03:54:31 +08:00
|
|
|
if (punc == ")") {
|
|
|
|
break;
|
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
if (isVariadic || punc != ",") {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(j),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#define: malformed argument list"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2018-03-17 07:13:49 +08:00
|
|
|
if (j == tokens) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(tokens - 1),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#define: malformed argument list"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (std::set<std::string>(argName.begin(), argName.end()).size() !=
|
|
|
|
argName.size()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenance(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#define: argument names are not distinct"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2018-02-06 06:29:26 +08:00
|
|
|
definitions_.emplace(std::make_pair(
|
|
|
|
nameToken, Definition{argName, dir, j, tokens - j, isVariadic}));
|
2018-01-31 03:54:31 +08:00
|
|
|
} else {
|
2018-05-12 03:54:49 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
2018-01-31 03:54:31 +08:00
|
|
|
definitions_.emplace(
|
2018-02-06 06:29:26 +08:00
|
|
|
std::make_pair(nameToken, Definition{dir, j, tokens - j}));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "undef") {
|
2018-01-31 03:54:31 +08:00
|
|
|
if (nameToken.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(
|
2018-08-09 02:29:05 +08:00
|
|
|
dir.GetIntervalProvenanceRange(dirOffset, tokens - dirOffset),
|
|
|
|
"# missing or invalid name"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
2020-07-18 02:21:08 +08:00
|
|
|
if (dir.IsAnythingLeft(++j)) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2022-03-08 05:57:37 +08:00
|
|
|
"#undef: excess tokens at end of directive"_port_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
|
|
|
definitions_.erase(nameToken);
|
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "ifdef" || dirName == "ifndef") {
|
2019-03-16 01:50:15 +08:00
|
|
|
bool doThen{false};
|
2018-01-31 03:54:31 +08:00
|
|
|
if (nameToken.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(
|
2018-08-09 02:29:05 +08:00
|
|
|
dir.GetIntervalProvenanceRange(dirOffset, tokens - dirOffset),
|
2019-05-07 00:33:45 +08:00
|
|
|
"#%s: missing name"_err_en_US, dirName);
|
2019-03-16 01:50:15 +08:00
|
|
|
} else {
|
2020-07-18 02:21:08 +08:00
|
|
|
if (dir.IsAnythingLeft(++j)) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2022-03-08 05:57:37 +08:00
|
|
|
"#%s: excess tokens at end of directive"_port_en_US, dirName);
|
2019-03-16 01:50:15 +08:00
|
|
|
}
|
|
|
|
doThen = IsNameDefined(nameToken) == (dirName == "ifdef");
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2019-03-16 01:50:15 +08:00
|
|
|
if (doThen) {
|
2018-02-02 04:08:02 +08:00
|
|
|
ifStack_.push(CanDeadElseAppear::Yes);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
2018-03-20 02:48:49 +08:00
|
|
|
SkipDisabledConditionalCode(dirName, IsElseActive::Yes, prescanner,
|
|
|
|
dir.GetTokenProvenance(dirOffset));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "if") {
|
2018-02-14 04:24:54 +08:00
|
|
|
if (IsIfPredicateTrue(dir, j, tokens - j, prescanner)) {
|
2018-02-02 04:08:02 +08:00
|
|
|
ifStack_.push(CanDeadElseAppear::Yes);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
2018-03-20 02:48:49 +08:00
|
|
|
SkipDisabledConditionalCode(dirName, IsElseActive::Yes, prescanner,
|
2018-04-24 03:17:11 +08:00
|
|
|
dir.GetTokenProvenanceRange(dirOffset));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "else") {
|
2020-07-18 02:21:08 +08:00
|
|
|
if (dir.IsAnythingLeft(j)) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2022-03-08 05:57:37 +08:00
|
|
|
"#else: excess tokens at end of directive"_port_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (ifStack_.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#else: not nested within #if, #ifdef, or #ifndef"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (ifStack_.top() != CanDeadElseAppear::Yes) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#else: already appeared within this #if, #ifdef, or #ifndef"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
|
|
|
ifStack_.pop();
|
2018-03-20 02:48:49 +08:00
|
|
|
SkipDisabledConditionalCode("else", IsElseActive::No, prescanner,
|
2018-04-24 03:17:11 +08:00
|
|
|
dir.GetTokenProvenanceRange(dirOffset));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "elif") {
|
2018-02-02 04:08:02 +08:00
|
|
|
if (ifStack_.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#elif: not nested within #if, #ifdef, or #ifndef"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (ifStack_.top() != CanDeadElseAppear::Yes) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#elif: #else previously appeared within this #if, #ifdef, or #ifndef"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
|
|
|
ifStack_.pop();
|
2018-03-20 02:48:49 +08:00
|
|
|
SkipDisabledConditionalCode("elif", IsElseActive::No, prescanner,
|
2018-04-24 03:17:11 +08:00
|
|
|
dir.GetTokenProvenanceRange(dirOffset));
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "endif") {
|
2020-07-18 02:21:08 +08:00
|
|
|
if (dir.IsAnythingLeft(j)) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2022-03-08 05:57:37 +08:00
|
|
|
"#endif: excess tokens at end of directive"_port_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (ifStack_.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#endif: no #if, #ifdef, or #ifndef"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
|
|
|
ifStack_.pop();
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "error") {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(
|
2018-08-09 02:29:05 +08:00
|
|
|
dir.GetIntervalProvenanceRange(dirOffset, tokens - dirOffset),
|
2019-05-07 00:33:45 +08:00
|
|
|
"%s"_err_en_US, dir.ToString());
|
2022-03-08 05:57:37 +08:00
|
|
|
} else if (dirName == "warning") {
|
|
|
|
prescanner.Say(
|
|
|
|
dir.GetIntervalProvenanceRange(dirOffset, tokens - dirOffset),
|
|
|
|
"%s"_warn_en_US, dir.ToString());
|
|
|
|
} else if (dirName == "comment" || dirName == "note") {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(
|
2018-08-09 02:29:05 +08:00
|
|
|
dir.GetIntervalProvenanceRange(dirOffset, tokens - dirOffset),
|
2019-05-07 00:33:45 +08:00
|
|
|
"%s"_en_US, dir.ToString());
|
2018-03-17 07:13:49 +08:00
|
|
|
} else if (dirName == "include") {
|
2018-02-14 08:33:23 +08:00
|
|
|
if (j == tokens) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(
|
2018-08-09 02:29:05 +08:00
|
|
|
dir.GetIntervalProvenanceRange(dirOffset, tokens - dirOffset),
|
|
|
|
"#include: missing name of file to include"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-14 08:33:23 +08:00
|
|
|
}
|
|
|
|
std::string include;
|
2021-01-27 05:57:44 +08:00
|
|
|
std::optional<std::string> prependPath;
|
2020-09-15 05:09:01 +08:00
|
|
|
if (dir.TokenAt(j).ToString() == "<") { // #include <foo>
|
2019-03-16 06:01:54 +08:00
|
|
|
std::size_t k{j + 1};
|
|
|
|
if (k >= tokens) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2019-03-16 06:01:54 +08:00
|
|
|
"#include: file name missing"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-14 08:33:23 +08:00
|
|
|
}
|
2019-03-16 06:01:54 +08:00
|
|
|
while (k < tokens && dir.TokenAt(k) != ">") {
|
|
|
|
++k;
|
|
|
|
}
|
|
|
|
if (k >= tokens) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2022-03-08 05:57:37 +08:00
|
|
|
"#include: expected '>' at end of included file"_port_en_US);
|
2019-03-16 06:01:54 +08:00
|
|
|
}
|
|
|
|
TokenSequence braced{dir, j + 1, k - j - 1};
|
2021-09-01 08:16:01 +08:00
|
|
|
include = ReplaceMacros(braced, prescanner).ToString();
|
2020-09-15 05:09:01 +08:00
|
|
|
j = k;
|
|
|
|
} else if ((include = dir.TokenAt(j).ToString()).substr(0, 1) == "\"" &&
|
|
|
|
include.substr(include.size() - 1, 1) == "\"") { // #include "foo"
|
2018-02-14 08:33:23 +08:00
|
|
|
include = include.substr(1, include.size() - 2);
|
2021-01-27 05:57:44 +08:00
|
|
|
// #include "foo" starts search in directory of file containing
|
|
|
|
// the directive
|
|
|
|
auto prov{dir.GetTokenProvenanceRange(dirOffset).start()};
|
|
|
|
if (const auto *currentFile{allSources_.GetSourceFile(prov)}) {
|
|
|
|
prependPath = DirectoryName(currentFile->path());
|
|
|
|
}
|
2018-02-14 08:33:23 +08:00
|
|
|
} else {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(j < tokens ? j : tokens - 1),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#include: expected name of file to include"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-14 08:33:23 +08:00
|
|
|
}
|
|
|
|
if (include.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2018-08-09 02:29:05 +08:00
|
|
|
"#include: empty include file name"_err_en_US);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-14 08:33:23 +08:00
|
|
|
}
|
2020-09-15 05:09:01 +08:00
|
|
|
j = dir.SkipBlanks(j + 1);
|
|
|
|
if (j < tokens && dir.TokenAt(j).ToString() != "!") {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetIntervalProvenanceRange(j, tokens - j),
|
2022-03-08 05:57:37 +08:00
|
|
|
"#include: extra stuff ignored after file name"_port_en_US);
|
2020-09-15 05:09:01 +08:00
|
|
|
}
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream error{buf};
|
2021-01-27 05:57:44 +08:00
|
|
|
const SourceFile *included{
|
|
|
|
allSources_.Open(include, error, std::move(prependPath))};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!included) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2019-05-07 00:33:45 +08:00
|
|
|
"#include: %s"_err_en_US, error.str());
|
2018-04-04 23:06:15 +08:00
|
|
|
} else if (included->bytes() > 0) {
|
|
|
|
ProvenanceRange fileRange{
|
|
|
|
allSources_.AddIncludedFile(*included, dir.GetProvenanceRange())};
|
2021-09-01 08:16:01 +08:00
|
|
|
Prescanner{prescanner}
|
2019-06-13 06:26:37 +08:00
|
|
|
.set_encoding(included->encoding())
|
|
|
|
.Prescan(fileRange);
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
} else {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(dir.GetTokenProvenanceRange(dirOffset),
|
2019-05-07 00:33:45 +08:00
|
|
|
"#%s: unknown or unimplemented directive"_err_en_US, dirName);
|
2018-02-14 08:33:23 +08:00
|
|
|
}
|
2018-01-31 03:54:31 +08:00
|
|
|
}
|
2018-02-02 04:08:02 +08:00
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
CharBlock Preprocessor::SaveTokenAsName(const CharBlock &t) {
|
2018-02-02 07:01:23 +08:00
|
|
|
names_.push_back(t.ToString());
|
|
|
|
return {names_.back().data(), names_.back().size()};
|
|
|
|
}
|
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
bool Preprocessor::IsNameDefined(const CharBlock &token) {
|
2018-02-02 04:08:02 +08:00
|
|
|
return definitions_.find(token) != definitions_.end();
|
|
|
|
}
|
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
static std::string GetDirectiveName(
|
|
|
|
const TokenSequence &line, std::size_t *rest) {
|
2018-03-24 06:14:52 +08:00
|
|
|
std::size_t tokens{line.SizeInTokens()};
|
2018-05-12 03:54:49 +08:00
|
|
|
std::size_t j{line.SkipBlanks(0)};
|
2018-03-24 06:14:52 +08:00
|
|
|
if (j == tokens || line.TokenAt(j).ToString() != "#") {
|
2018-03-17 07:13:49 +08:00
|
|
|
*rest = tokens;
|
2018-03-17 07:58:35 +08:00
|
|
|
return "";
|
2018-03-17 07:13:49 +08:00
|
|
|
}
|
2018-05-12 03:54:49 +08:00
|
|
|
j = line.SkipBlanks(j + 1);
|
2018-03-17 07:13:49 +08:00
|
|
|
if (j == tokens) {
|
|
|
|
*rest = tokens;
|
2018-03-17 07:58:35 +08:00
|
|
|
return "";
|
2018-03-17 07:13:49 +08:00
|
|
|
}
|
2018-05-12 03:54:49 +08:00
|
|
|
*rest = line.SkipBlanks(j + 1);
|
2018-03-24 06:14:52 +08:00
|
|
|
return ToLowerCaseLetters(line.TokenAt(j).ToString());
|
2018-03-17 07:13:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::SkipDisabledConditionalCode(const std::string &dirName,
|
2021-09-01 08:16:01 +08:00
|
|
|
IsElseActive isElseActive, Prescanner &prescanner,
|
2018-04-24 03:17:11 +08:00
|
|
|
ProvenanceRange provenanceRange) {
|
2018-02-02 04:08:02 +08:00
|
|
|
int nesting{0};
|
2021-09-01 08:16:01 +08:00
|
|
|
while (!prescanner.IsAtEnd()) {
|
|
|
|
if (!prescanner.IsNextLinePreprocessorDirective()) {
|
|
|
|
prescanner.NextLine();
|
2018-03-17 07:58:35 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-09-01 08:16:01 +08:00
|
|
|
TokenSequence line{prescanner.TokenizePreprocessorDirective()};
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t rest{0};
|
2018-03-17 07:58:35 +08:00
|
|
|
std::string dn{GetDirectiveName(line, &rest)};
|
2018-02-02 04:08:02 +08:00
|
|
|
if (dn == "ifdef" || dn == "ifndef" || dn == "if") {
|
|
|
|
++nesting;
|
|
|
|
} else if (dn == "endif") {
|
|
|
|
if (nesting-- == 0) {
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2018-02-06 06:45:32 +08:00
|
|
|
} else if (isElseActive == IsElseActive::Yes && nesting == 0) {
|
|
|
|
if (dn == "else") {
|
|
|
|
ifStack_.push(CanDeadElseAppear::No);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-06 06:45:32 +08:00
|
|
|
}
|
2018-02-14 04:24:54 +08:00
|
|
|
if (dn == "elif" &&
|
2018-03-24 06:14:52 +08:00
|
|
|
IsIfPredicateTrue(
|
|
|
|
line, rest, line.SizeInTokens() - rest, prescanner)) {
|
2018-02-06 06:45:32 +08:00
|
|
|
ifStack_.push(CanDeadElseAppear::Yes);
|
2018-03-17 07:13:49 +08:00
|
|
|
return;
|
2018-02-06 06:45:32 +08:00
|
|
|
}
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(provenanceRange, "#%s: missing #endif"_err_en_US, dirName);
|
2018-02-03 07:52:43 +08:00
|
|
|
}
|
|
|
|
|
2018-02-02 04:08:02 +08:00
|
|
|
// Precedence level codes used here to accommodate mixed Fortran and C:
|
2018-02-06 04:48:09 +08:00
|
|
|
// 15: parentheses and constants, logical !, bitwise ~
|
|
|
|
// 14: unary + and -
|
|
|
|
// 13: **
|
|
|
|
// 12: *, /, % (modulus)
|
|
|
|
// 11: + and -
|
2018-02-13 06:43:16 +08:00
|
|
|
// 10: << and >>
|
2018-02-06 04:48:09 +08:00
|
|
|
// 9: bitwise &
|
|
|
|
// 8: bitwise ^
|
|
|
|
// 7: bitwise |
|
|
|
|
// 6: relations (.EQ., ==, &c.)
|
|
|
|
// 5: .NOT.
|
|
|
|
// 4: .AND., &&
|
|
|
|
// 3: .OR., ||
|
|
|
|
// 2: .EQV. and .NEQV. / .XOR.
|
|
|
|
// 1: ? :
|
|
|
|
// 0: ,
|
2018-02-02 04:08:02 +08:00
|
|
|
static std::int64_t ExpressionValue(const TokenSequence &token,
|
2018-03-21 01:59:07 +08:00
|
|
|
int minimumPrecedence, std::size_t *atToken,
|
|
|
|
std::optional<Message> *error) {
|
2018-02-02 04:08:02 +08:00
|
|
|
enum Operator {
|
2018-02-06 06:29:26 +08:00
|
|
|
PARENS,
|
|
|
|
CONST,
|
2020-03-29 12:00:16 +08:00
|
|
|
NOTZERO, // !
|
|
|
|
COMPLEMENT, // ~
|
2018-02-06 06:29:26 +08:00
|
|
|
UPLUS,
|
|
|
|
UMINUS,
|
|
|
|
POWER,
|
|
|
|
TIMES,
|
|
|
|
DIVIDE,
|
|
|
|
MODULUS,
|
|
|
|
ADD,
|
|
|
|
SUBTRACT,
|
|
|
|
LEFTSHIFT,
|
|
|
|
RIGHTSHIFT,
|
|
|
|
BITAND,
|
|
|
|
BITXOR,
|
|
|
|
BITOR,
|
|
|
|
LT,
|
|
|
|
LE,
|
|
|
|
EQ,
|
|
|
|
NE,
|
|
|
|
GE,
|
|
|
|
GT,
|
|
|
|
NOT,
|
|
|
|
AND,
|
|
|
|
OR,
|
|
|
|
EQV,
|
|
|
|
NEQV,
|
|
|
|
SELECT,
|
|
|
|
COMMA
|
2018-02-02 04:08:02 +08:00
|
|
|
};
|
|
|
|
static const int precedence[]{
|
2020-03-29 12:00:16 +08:00
|
|
|
15, 15, 15, 15, // (), 6, !, ~
|
|
|
|
14, 14, // unary +, -
|
|
|
|
13, 12, 12, 12, 11, 11, 10, 10, // **, *, /, %, +, -, <<, >>
|
|
|
|
9, 8, 7, // &, ^, |
|
|
|
|
6, 6, 6, 6, 6, 6, // relations .LT. to .GT.
|
|
|
|
5, 4, 3, 2, 2, // .NOT., .AND., .OR., .EQV., .NEQV.
|
|
|
|
1, 0 // ?: and ,
|
2018-02-02 04:08:02 +08:00
|
|
|
};
|
2018-02-06 06:29:26 +08:00
|
|
|
static const int operandPrecedence[]{0, -1, 15, 15, 15, 15, 13, 12, 12, 12,
|
|
|
|
11, 11, 11, 11, 9, 8, 7, 7, 7, 7, 7, 7, 7, 6, 4, 3, 3, 3, 1, 0};
|
2018-02-02 04:08:02 +08:00
|
|
|
|
|
|
|
static std::map<std::string, enum Operator> opNameMap;
|
|
|
|
if (opNameMap.empty()) {
|
|
|
|
opNameMap["("] = PARENS;
|
|
|
|
opNameMap["!"] = NOTZERO;
|
|
|
|
opNameMap["~"] = COMPLEMENT;
|
|
|
|
opNameMap["**"] = POWER;
|
|
|
|
opNameMap["*"] = TIMES;
|
|
|
|
opNameMap["/"] = DIVIDE;
|
|
|
|
opNameMap["%"] = MODULUS;
|
|
|
|
opNameMap["+"] = ADD;
|
|
|
|
opNameMap["-"] = SUBTRACT;
|
|
|
|
opNameMap["<<"] = LEFTSHIFT;
|
|
|
|
opNameMap[">>"] = RIGHTSHIFT;
|
|
|
|
opNameMap["&"] = BITAND;
|
|
|
|
opNameMap["^"] = BITXOR;
|
|
|
|
opNameMap["|"] = BITOR;
|
|
|
|
opNameMap[".lt."] = opNameMap["<"] = LT;
|
|
|
|
opNameMap[".le."] = opNameMap["<="] = LE;
|
|
|
|
opNameMap[".eq."] = opNameMap["=="] = EQ;
|
|
|
|
opNameMap[".ne."] = opNameMap["/="] = opNameMap["!="] = NE;
|
|
|
|
opNameMap[".ge."] = opNameMap[">="] = GE;
|
|
|
|
opNameMap[".gt."] = opNameMap[">"] = GT;
|
|
|
|
opNameMap[".not."] = NOT;
|
|
|
|
opNameMap[".and."] = opNameMap[".a."] = opNameMap["&&"] = AND;
|
|
|
|
opNameMap[".or."] = opNameMap[".o."] = opNameMap["||"] = OR;
|
|
|
|
opNameMap[".eqv."] = EQV;
|
|
|
|
opNameMap[".neqv."] = opNameMap[".xor."] = opNameMap[".x."] = NEQV;
|
2018-02-06 04:48:09 +08:00
|
|
|
opNameMap["?"] = SELECT;
|
|
|
|
opNameMap[","] = COMMA;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
std::size_t tokens{token.SizeInTokens()};
|
2018-05-04 02:09:04 +08:00
|
|
|
CHECK(tokens > 0);
|
2018-02-02 04:08:02 +08:00
|
|
|
if (*atToken >= tokens) {
|
2018-04-24 03:17:11 +08:00
|
|
|
*error =
|
|
|
|
Message{token.GetProvenanceRange(), "incomplete expression"_err_en_US};
|
2018-02-02 04:08:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse and evaluate a primary or a unary operator and its operand.
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t opAt{*atToken};
|
2018-03-24 06:14:52 +08:00
|
|
|
std::string t{token.TokenAt(opAt).ToString()};
|
2018-03-20 02:48:49 +08:00
|
|
|
enum Operator op;
|
2018-02-02 04:08:02 +08:00
|
|
|
std::int64_t left{0};
|
|
|
|
if (t == "(") {
|
|
|
|
op = PARENS;
|
2018-02-28 06:02:10 +08:00
|
|
|
} else if (IsDecimalDigit(t[0])) {
|
2018-02-02 04:08:02 +08:00
|
|
|
op = CONST;
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t consumed{0};
|
2018-03-20 02:48:49 +08:00
|
|
|
left = std::stoll(t, &consumed, 0 /*base to be detected*/);
|
2018-02-02 04:08:02 +08:00
|
|
|
if (consumed < t.size()) {
|
2018-04-24 03:17:11 +08:00
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
2019-05-07 00:33:45 +08:00
|
|
|
"Uninterpretable numeric constant '%s'"_err_en_US, t};
|
2018-03-20 02:48:49 +08:00
|
|
|
return 0;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2018-02-28 06:02:10 +08:00
|
|
|
} else if (IsLegalIdentifierStart(t[0])) {
|
2018-02-02 04:08:02 +08:00
|
|
|
// undefined macro name -> zero
|
|
|
|
// TODO: BOZ constants?
|
|
|
|
op = CONST;
|
|
|
|
} else if (t == "+") {
|
|
|
|
op = UPLUS;
|
|
|
|
} else if (t == "-") {
|
|
|
|
op = UMINUS;
|
|
|
|
} else if (t == "." && *atToken + 2 < tokens &&
|
2018-03-24 06:14:52 +08:00
|
|
|
ToLowerCaseLetters(token.TokenAt(*atToken + 1).ToString()) == "not" &&
|
|
|
|
token.TokenAt(*atToken + 2).ToString() == ".") {
|
2018-02-02 04:08:02 +08:00
|
|
|
op = NOT;
|
|
|
|
*atToken += 2;
|
|
|
|
} else {
|
2018-07-11 08:09:07 +08:00
|
|
|
auto it{opNameMap.find(t)};
|
2018-02-02 04:08:02 +08:00
|
|
|
if (it != opNameMap.end()) {
|
|
|
|
op = it->second;
|
|
|
|
} else {
|
2018-04-24 03:17:11 +08:00
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
2018-04-03 06:51:04 +08:00
|
|
|
"operand expected in expression"_err_en_US};
|
2018-02-02 04:08:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 02:48:49 +08:00
|
|
|
if (precedence[op] < minimumPrecedence) {
|
2018-04-24 03:17:11 +08:00
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"operator precedence error"_err_en_US};
|
2018-03-20 02:48:49 +08:00
|
|
|
return 0;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
++*atToken;
|
2018-03-20 02:48:49 +08:00
|
|
|
if (op != CONST) {
|
2018-02-17 08:57:40 +08:00
|
|
|
left = ExpressionValue(token, operandPrecedence[op], atToken, error);
|
2019-11-10 01:29:31 +08:00
|
|
|
if (*error) {
|
2018-03-20 02:48:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-02 04:08:02 +08:00
|
|
|
switch (op) {
|
|
|
|
case PARENS:
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*atToken < tokens && token.TokenAt(*atToken).ToString() == ")") {
|
2018-02-02 04:08:02 +08:00
|
|
|
++*atToken;
|
2018-04-24 03:17:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*atToken >= tokens) {
|
|
|
|
*error = Message{token.GetProvenanceRange(),
|
2018-04-03 06:51:04 +08:00
|
|
|
"')' missing from expression"_err_en_US};
|
2018-04-24 03:17:11 +08:00
|
|
|
} else {
|
|
|
|
*error = Message{
|
|
|
|
token.GetTokenProvenanceRange(*atToken), "expected ')'"_err_en_US};
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2018-04-24 03:17:11 +08:00
|
|
|
return 0;
|
2020-03-29 12:00:16 +08:00
|
|
|
case NOTZERO:
|
|
|
|
left = !left;
|
|
|
|
break;
|
|
|
|
case COMPLEMENT:
|
|
|
|
left = ~left;
|
|
|
|
break;
|
|
|
|
case UPLUS:
|
|
|
|
break;
|
|
|
|
case UMINUS:
|
|
|
|
left = -left;
|
|
|
|
break;
|
|
|
|
case NOT:
|
|
|
|
left = -!left;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CRASH_NO_CASE;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 03:19:26 +08:00
|
|
|
// Parse and evaluate binary operators and their second operands, if present.
|
|
|
|
while (*atToken < tokens) {
|
|
|
|
int advance{1};
|
|
|
|
t = token.TokenAt(*atToken).ToString();
|
|
|
|
if (t == "." && *atToken + 2 < tokens &&
|
|
|
|
token.TokenAt(*atToken + 2).ToString() == ".") {
|
|
|
|
t += ToLowerCaseLetters(token.TokenAt(*atToken + 1).ToString()) + '.';
|
|
|
|
advance = 3;
|
|
|
|
}
|
|
|
|
auto it{opNameMap.find(t)};
|
|
|
|
if (it == opNameMap.end()) {
|
|
|
|
break;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2019-03-15 03:19:26 +08:00
|
|
|
op = it->second;
|
|
|
|
if (op < POWER || precedence[op] < minimumPrecedence) {
|
|
|
|
break;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2019-03-15 03:19:26 +08:00
|
|
|
opAt = *atToken;
|
|
|
|
*atToken += advance;
|
|
|
|
|
|
|
|
std::int64_t right{
|
|
|
|
ExpressionValue(token, operandPrecedence[op], atToken, error)};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (*error) {
|
2019-03-15 03:19:26 +08:00
|
|
|
return 0;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2019-03-15 03:19:26 +08:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case POWER:
|
|
|
|
if (left == 0) {
|
|
|
|
if (right < 0) {
|
2018-04-24 03:17:11 +08:00
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
2019-03-15 03:19:26 +08:00
|
|
|
"0 ** negative power"_err_en_US};
|
|
|
|
}
|
|
|
|
} else if (left != 1 && right != 1) {
|
|
|
|
if (right <= 0) {
|
|
|
|
left = !right;
|
|
|
|
} else {
|
|
|
|
std::int64_t power{1};
|
|
|
|
for (; right > 0; --right) {
|
|
|
|
if ((power * left) / left != power) {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"overflow in exponentation"_err_en_US};
|
|
|
|
left = 1;
|
|
|
|
}
|
|
|
|
power *= left;
|
|
|
|
}
|
|
|
|
left = power;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 03:19:26 +08:00
|
|
|
break;
|
|
|
|
case TIMES:
|
|
|
|
if (left != 0 && right != 0 && ((left * right) / left) != right) {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"overflow in multiplication"_err_en_US};
|
|
|
|
}
|
|
|
|
left = left * right;
|
|
|
|
break;
|
|
|
|
case DIVIDE:
|
|
|
|
if (right == 0) {
|
|
|
|
*error = Message{
|
|
|
|
token.GetTokenProvenanceRange(opAt), "division by zero"_err_en_US};
|
|
|
|
left = 0;
|
|
|
|
} else {
|
|
|
|
left = left / right;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MODULUS:
|
|
|
|
if (right == 0) {
|
|
|
|
*error = Message{
|
|
|
|
token.GetTokenProvenanceRange(opAt), "modulus by zero"_err_en_US};
|
|
|
|
left = 0;
|
|
|
|
} else {
|
|
|
|
left = left % right;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ADD:
|
|
|
|
if ((left < 0) == (right < 0) && (left < 0) != (left + right < 0)) {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"overflow in addition"_err_en_US};
|
|
|
|
}
|
|
|
|
left = left + right;
|
|
|
|
break;
|
|
|
|
case SUBTRACT:
|
|
|
|
if ((left < 0) != (right < 0) && (left < 0) == (left - right < 0)) {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"overflow in subtraction"_err_en_US};
|
|
|
|
}
|
|
|
|
left = left - right;
|
|
|
|
break;
|
|
|
|
case LEFTSHIFT:
|
|
|
|
if (right < 0 || right > 64) {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"bad left shift count"_err_en_US};
|
|
|
|
}
|
|
|
|
left = right >= 64 ? 0 : left << right;
|
|
|
|
break;
|
|
|
|
case RIGHTSHIFT:
|
|
|
|
if (right < 0 || right > 64) {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"bad right shift count"_err_en_US};
|
|
|
|
}
|
|
|
|
left = right >= 64 ? 0 : left >> right;
|
|
|
|
break;
|
|
|
|
case BITAND:
|
2020-03-29 12:00:16 +08:00
|
|
|
case AND:
|
|
|
|
left = left & right;
|
|
|
|
break;
|
|
|
|
case BITXOR:
|
|
|
|
left = left ^ right;
|
|
|
|
break;
|
2019-03-15 03:19:26 +08:00
|
|
|
case BITOR:
|
2020-03-29 12:00:16 +08:00
|
|
|
case OR:
|
|
|
|
left = left | right;
|
|
|
|
break;
|
|
|
|
case LT:
|
|
|
|
left = -(left < right);
|
|
|
|
break;
|
|
|
|
case LE:
|
|
|
|
left = -(left <= right);
|
|
|
|
break;
|
|
|
|
case EQ:
|
|
|
|
left = -(left == right);
|
|
|
|
break;
|
|
|
|
case NE:
|
|
|
|
left = -(left != right);
|
|
|
|
break;
|
|
|
|
case GE:
|
|
|
|
left = -(left >= right);
|
|
|
|
break;
|
|
|
|
case GT:
|
|
|
|
left = -(left > right);
|
|
|
|
break;
|
|
|
|
case EQV:
|
|
|
|
left = -(!left == !right);
|
|
|
|
break;
|
|
|
|
case NEQV:
|
|
|
|
left = -(!left != !right);
|
|
|
|
break;
|
2019-03-15 03:19:26 +08:00
|
|
|
case SELECT:
|
|
|
|
if (*atToken >= tokens || token.TokenAt(*atToken).ToString() != ":") {
|
|
|
|
*error = Message{token.GetTokenProvenanceRange(opAt),
|
|
|
|
"':' required in selection expression"_err_en_US};
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
++*atToken;
|
|
|
|
std::int64_t third{
|
|
|
|
ExpressionValue(token, operandPrecedence[op], atToken, error)};
|
|
|
|
left = left != 0 ? right : third;
|
|
|
|
}
|
2019-08-16 01:17:31 +08:00
|
|
|
break;
|
2020-03-29 12:00:16 +08:00
|
|
|
case COMMA:
|
|
|
|
left = right;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CRASH_NO_CASE;
|
2018-02-06 04:48:09 +08:00
|
|
|
}
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2019-03-15 03:19:26 +08:00
|
|
|
return left;
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
bool Preprocessor::IsIfPredicateTrue(const TokenSequence &expr,
|
2021-09-01 08:16:01 +08:00
|
|
|
std::size_t first, std::size_t exprTokens, Prescanner &prescanner) {
|
2018-05-12 03:44:56 +08:00
|
|
|
TokenSequence expr1{expr, first, exprTokens};
|
|
|
|
if (expr1.HasBlanks()) {
|
|
|
|
expr1.RemoveBlanks();
|
|
|
|
}
|
2018-02-02 04:08:02 +08:00
|
|
|
TokenSequence expr2;
|
2018-03-24 06:14:52 +08:00
|
|
|
for (std::size_t j{0}; j < expr1.SizeInTokens(); ++j) {
|
|
|
|
if (ToLowerCaseLetters(expr1.TokenAt(j).ToString()) == "defined") {
|
2018-03-21 01:59:07 +08:00
|
|
|
CharBlock name;
|
2018-03-24 06:14:52 +08:00
|
|
|
if (j + 3 < expr1.SizeInTokens() &&
|
|
|
|
expr1.TokenAt(j + 1).ToString() == "(" &&
|
|
|
|
expr1.TokenAt(j + 3).ToString() == ")") {
|
|
|
|
name = expr1.TokenAt(j + 2);
|
2018-02-02 04:08:02 +08:00
|
|
|
j += 3;
|
2018-03-24 06:14:52 +08:00
|
|
|
} else if (j + 1 < expr1.SizeInTokens() &&
|
|
|
|
IsLegalIdentifierStart(expr1.TokenAt(j + 1))) {
|
2019-12-21 05:03:30 +08:00
|
|
|
name = expr1.TokenAt(++j);
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
if (!name.empty()) {
|
2018-02-13 06:43:16 +08:00
|
|
|
char truth{IsNameDefined(name) ? '1' : '0'};
|
2018-03-24 06:14:52 +08:00
|
|
|
expr2.Put(&truth, 1, allSources_.CompilerInsertionProvenance(truth));
|
2018-02-02 04:08:02 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 06:04:11 +08:00
|
|
|
expr2.Put(expr1, j);
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
2021-09-01 08:16:01 +08:00
|
|
|
TokenSequence expr3{ReplaceMacros(expr2, prescanner)};
|
2018-05-12 03:44:56 +08:00
|
|
|
if (expr3.HasBlanks()) {
|
|
|
|
expr3.RemoveBlanks();
|
|
|
|
}
|
|
|
|
if (expr3.empty()) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(expr.GetProvenanceRange(), "empty expression"_err_en_US);
|
2018-05-04 02:09:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t atToken{0};
|
2018-03-20 02:48:49 +08:00
|
|
|
std::optional<Message> error;
|
2018-05-12 03:44:56 +08:00
|
|
|
bool result{ExpressionValue(expr3, 0, &atToken, &error) != 0};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (error) {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(std::move(*error));
|
2018-11-15 07:44:17 +08:00
|
|
|
} else if (atToken < expr3.SizeInTokens() &&
|
|
|
|
expr3.TokenAt(atToken).ToString() != "!") {
|
2021-09-01 08:16:01 +08:00
|
|
|
prescanner.Say(expr3.GetIntervalProvenanceRange(
|
|
|
|
atToken, expr3.SizeInTokens() - atToken),
|
2018-08-09 02:29:05 +08:00
|
|
|
atToken == 0 ? "could not parse any expression"_err_en_US
|
|
|
|
: "excess characters after expression"_err_en_US);
|
2018-02-02 04:08:02 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::parser
|