2015-07-22 21:03:00 +03:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2015 Kitware, Inc., Insight Software Consortium
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
#include "cmFortranParser.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
bool cmFortranParser_s::FindIncludeFile(const char* dir,
|
|
|
|
const char* includeName,
|
|
|
|
std::string& fileName)
|
|
|
|
{
|
|
|
|
// If the file is a full path, include it directly.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileIsFullPath(includeName)) {
|
2015-07-22 21:03:00 +03:00
|
|
|
fileName = includeName;
|
|
|
|
return cmSystemTools::FileExists(fileName.c_str(), true);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-22 21:03:00 +03:00
|
|
|
// Check for the file in the directory containing the including
|
|
|
|
// file.
|
|
|
|
std::string fullName = dir;
|
|
|
|
fullName += "/";
|
|
|
|
fullName += includeName;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileExists(fullName.c_str(), true)) {
|
2015-07-22 21:03:00 +03:00
|
|
|
fileName = fullName;
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
|
|
|
|
// Search the include path for the file.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator i =
|
|
|
|
this->IncludePath.begin();
|
|
|
|
i != this->IncludePath.end(); ++i) {
|
2015-07-22 21:03:00 +03:00
|
|
|
fullName = *i;
|
|
|
|
fullName += "/";
|
|
|
|
fullName += includeName;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileExists(fullName.c_str(), true)) {
|
2015-07-22 21:03:00 +03:00
|
|
|
fileName = fullName;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFortranParser_s::cmFortranParser_s(std::vector<std::string> const& includes,
|
|
|
|
std::set<std::string> const& defines,
|
|
|
|
cmFortranSourceInfo& info)
|
|
|
|
: IncludePath(includes)
|
|
|
|
, PPDefinitions(defines)
|
|
|
|
, Info(info)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
this->InInterface = 0;
|
|
|
|
this->InPPFalseBranch = 0;
|
|
|
|
|
|
|
|
// Initialize the lexical scanner.
|
|
|
|
cmFortran_yylex_init(&this->Scanner);
|
|
|
|
cmFortran_yyset_extra(this, this->Scanner);
|
|
|
|
|
|
|
|
// Create a dummy buffer that is never read but is the fallback
|
|
|
|
// buffer when the last file is popped off the stack.
|
2016-05-16 17:34:04 +03:00
|
|
|
YY_BUFFER_STATE buffer = cmFortran_yy_create_buffer(0, 4, this->Scanner);
|
2015-07-22 21:03:00 +03:00
|
|
|
cmFortran_yy_switch_to_buffer(buffer, this->Scanner);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFortranParser_s::~cmFortranParser_s()
|
|
|
|
{
|
|
|
|
cmFortran_yylex_destroy(this->Scanner);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmFortranParser_FilePush(cmFortranParser* parser, const char* fname)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
// Open the new file and push it onto the stack. Save the old
|
|
|
|
// buffer with it on the stack.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (FILE* file = cmsys::SystemTools::Fopen(fname, "rb")) {
|
|
|
|
YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
|
2015-07-22 21:03:00 +03:00
|
|
|
std::string dir = cmSystemTools::GetParentDirectory(fname);
|
|
|
|
cmFortranFile f(file, current, dir);
|
|
|
|
YY_BUFFER_STATE buffer =
|
|
|
|
cmFortran_yy_create_buffer(0, 16384, parser->Scanner);
|
|
|
|
cmFortran_yy_switch_to_buffer(buffer, parser->Scanner);
|
|
|
|
parser->FileStack.push(f);
|
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-22 21:03:00 +03:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFortranParser_FilePop(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
// Pop one file off the stack and close it. Switch the lexer back
|
|
|
|
// to the next one on the stack.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->FileStack.empty()) {
|
2015-07-22 21:03:00 +03:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
cmFortranFile f = parser->FileStack.top();
|
|
|
|
parser->FileStack.pop();
|
2015-07-22 21:03:00 +03:00
|
|
|
fclose(f.File);
|
2016-05-16 17:34:04 +03:00
|
|
|
YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
|
2015-07-22 21:03:00 +03:00
|
|
|
cmFortran_yy_delete_buffer(current, parser->Scanner);
|
|
|
|
cmFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
|
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
|
|
|
|
size_t bufferSize)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
// Read from the file on top of the stack. If the stack is empty,
|
|
|
|
// the end of the translation unit has been reached.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->FileStack.empty()) {
|
2015-07-22 21:03:00 +03:00
|
|
|
FILE* file = parser->FileStack.top().File;
|
|
|
|
return (int)fread(buffer, 1, bufferSize, file);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmFortranParser_StringStart(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
parser->TokenString = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmFortranParser_StringEnd(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
return parser->TokenString.c_str();
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_StringAppend(cmFortranParser* parser, char c)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
parser->TokenString += c;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_SetInInterface(cmFortranParser* parser, bool in)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
|
|
|
|
parser->InInterface = in;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmFortranParser_GetInInterface(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
return parser->InInterface;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
parser->OldStartcond = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmFortranParser_GetOldStartcond(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
return parser->OldStartcond;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmFortranParser_Error(cmFortranParser*, const char*)
|
|
|
|
{
|
|
|
|
// If there is a parser error just ignore it. The source will not
|
|
|
|
// compile and the user will edit it. Then dependencies will have
|
|
|
|
// to be regenerated anyway.
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleUse(cmFortranParser* parser, const char* name)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->InPPFalseBranch) {
|
|
|
|
parser->Info.Requires.insert(cmSystemTools::LowerCase(name));
|
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 23:29:52 +03:00
|
|
|
void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
|
|
|
|
const char* filename)
|
|
|
|
{
|
|
|
|
// This is a #line directive naming a file encountered during preprocessing.
|
|
|
|
std::string included = filename;
|
|
|
|
|
|
|
|
// Skip #line directives referencing non-files like
|
|
|
|
// "<built-in>" or "<command-line>".
|
2016-05-16 17:34:04 +03:00
|
|
|
if (included.empty() || included[0] == '<') {
|
2015-11-02 23:29:52 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-02 23:29:52 +03:00
|
|
|
|
|
|
|
// Fix windows file path separators since our lexer does not
|
|
|
|
// process escape sequences in string literals.
|
|
|
|
cmSystemTools::ReplaceString(included, "\\\\", "\\");
|
|
|
|
cmSystemTools::ConvertToUnixSlashes(included);
|
|
|
|
|
|
|
|
// Save the named file as included in the source.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileExists(included, true)) {
|
2015-11-02 23:29:52 +03:00
|
|
|
parser->Info.Includes.insert(included);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-02 23:29:52 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
|
|
|
|
// If processing an include statement there must be an open file.
|
|
|
|
assert(!parser->FileStack.empty());
|
|
|
|
|
|
|
|
// Get the directory containing the source in which the include
|
|
|
|
// statement appears. This is always the first search location for
|
|
|
|
// Fortran include files.
|
|
|
|
std::string dir = parser->FileStack.top().Directory;
|
|
|
|
|
|
|
|
// Find the included file. If it cannot be found just ignore the
|
|
|
|
// problem because either the source will not compile or the user
|
|
|
|
// does not care about depending on this included source.
|
|
|
|
std::string fullName;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->FindIncludeFile(dir.c_str(), name, fullName)) {
|
2015-07-22 21:03:00 +03:00
|
|
|
// Found the included file. Save it in the set of included files.
|
|
|
|
parser->Info.Includes.insert(fullName);
|
|
|
|
|
|
|
|
// Parse it immediately to translate the source inline.
|
|
|
|
cmFortranParser_FilePush(parser, fullName.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleModule(cmFortranParser* parser, const char* name)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->InPPFalseBranch && !parser->InInterface) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* macro)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->PPDefinitions.insert(macro);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* macro)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
std::set<std::string>::iterator match;
|
|
|
|
match = parser->PPDefinitions.find(macro);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (match != parser->PPDefinitions.end()) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->PPDefinitions.erase(match);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* macro)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
// A new PP branch has been opened
|
|
|
|
parser->SkipToEnd.push(false);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch++;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (parser->PPDefinitions.find(macro) ==
|
|
|
|
parser->PPDefinitions.end()) {
|
|
|
|
parser->InPPFalseBranch = 1;
|
|
|
|
} else {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->SkipToEnd.top() = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* macro)
|
2015-07-22 21:03:00 +03:00
|
|
|
{
|
|
|
|
// A new PP branch has been opened
|
|
|
|
parser->SkipToEnd.push(false);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch++;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (parser->PPDefinitions.find(macro) !=
|
|
|
|
parser->PPDefinitions.end()) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch = 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-22 21:03:00 +03:00
|
|
|
// ignore other branches
|
|
|
|
parser->SkipToEnd.top() = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmFortranParser_RuleIf(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
/* Note: The current parser is _not_ able to get statements like
|
|
|
|
* #if 0
|
|
|
|
* #if 1
|
|
|
|
* #if MYSMBOL
|
|
|
|
* #if defined(MYSYMBOL)
|
|
|
|
* #if defined(MYSYMBOL) && ...
|
|
|
|
* right. The same for #elif. Thus in
|
|
|
|
* #if SYMBOL_1
|
|
|
|
* ..
|
|
|
|
* #elif SYMBOL_2
|
|
|
|
* ...
|
|
|
|
* ...
|
|
|
|
* #elif SYMBOL_N
|
|
|
|
* ..
|
|
|
|
* #else
|
|
|
|
* ..
|
|
|
|
* #endif
|
|
|
|
* _all_ N+1 branches are considered. If you got something like this
|
|
|
|
* #if defined(MYSYMBOL)
|
|
|
|
* #if !defined(MYSYMBOL)
|
|
|
|
* use
|
|
|
|
* #ifdef MYSYMBOL
|
|
|
|
* #ifndef MYSYMBOL
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// A new PP branch has been opened
|
|
|
|
// Never skip! See note above.
|
|
|
|
parser->SkipToEnd.push(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmFortranParser_RuleElif(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
/* Note: There are parser limitations. See the note at
|
|
|
|
* cmFortranParser_RuleIf(..)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Always taken unless an #ifdef or #ifndef-branch has been taken
|
|
|
|
// already. If the second condition isn't meet already
|
|
|
|
// (parser->InPPFalseBranch == 0) correct it.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top() &&
|
|
|
|
!parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch = 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmFortranParser_RuleElse(cmFortranParser* parser)
|
|
|
|
{
|
|
|
|
// if the parent branch is false do nothing!
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->InPPFalseBranch > 1) {
|
2015-07-22 21:03:00 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
|
|
|
|
// parser->InPPFalseBranch is either 0 or 1. We change it depending on
|
|
|
|
// parser->SkipToEnd.top()
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top()) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch = 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmFortranParser_RuleEndif(cmFortranParser* parser)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!parser->SkipToEnd.empty()) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->SkipToEnd.pop();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
|
|
|
|
// #endif doesn't know if there was a "#else" in before, so it
|
|
|
|
// always decreases InPPFalseBranch
|
2016-05-16 17:34:04 +03:00
|
|
|
if (parser->InPPFalseBranch) {
|
2015-07-22 21:03:00 +03:00
|
|
|
parser->InPPFalseBranch--;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-22 21:03:00 +03:00
|
|
|
}
|