cmFortranParserImpl.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFortranParser.h"
  4. #include "cmStringAlgorithms.h"
  5. #include "cmSystemTools.h"
  6. #include <assert.h>
  7. #include <set>
  8. #include <stack>
  9. #include <stdio.h>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. bool cmFortranParser_s::FindIncludeFile(const char* dir,
  14. const char* includeName,
  15. std::string& fileName)
  16. {
  17. // If the file is a full path, include it directly.
  18. if (cmSystemTools::FileIsFullPath(includeName)) {
  19. fileName = includeName;
  20. return cmSystemTools::FileExists(fileName, true);
  21. }
  22. // Check for the file in the directory containing the including
  23. // file.
  24. std::string fullName = cmStrCat(dir, '/', includeName);
  25. if (cmSystemTools::FileExists(fullName, true)) {
  26. fileName = fullName;
  27. return true;
  28. }
  29. // Search the include path for the file.
  30. for (std::string const& i : this->IncludePath) {
  31. fullName = cmStrCat(i, '/', includeName);
  32. if (cmSystemTools::FileExists(fullName, true)) {
  33. fileName = fullName;
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. cmFortranParser_s::cmFortranParser_s(cmFortranCompiler fc,
  40. std::vector<std::string> includes,
  41. std::set<std::string> defines,
  42. cmFortranSourceInfo& info)
  43. : Compiler(std::move(fc))
  44. , IncludePath(std::move(includes))
  45. , PPDefinitions(std::move(defines))
  46. , Info(info)
  47. {
  48. this->InInterface = false;
  49. this->InPPFalseBranch = 0;
  50. // Initialize the lexical scanner.
  51. cmFortran_yylex_init(&this->Scanner);
  52. cmFortran_yyset_extra(this, this->Scanner);
  53. // Create a dummy buffer that is never read but is the fallback
  54. // buffer when the last file is popped off the stack.
  55. YY_BUFFER_STATE buffer =
  56. cmFortran_yy_create_buffer(nullptr, 4, this->Scanner);
  57. cmFortran_yy_switch_to_buffer(buffer, this->Scanner);
  58. }
  59. cmFortranParser_s::~cmFortranParser_s()
  60. {
  61. cmFortran_yylex_destroy(this->Scanner);
  62. }
  63. std::string cmFortranParser_s::ModName(std::string const& mod_name) const
  64. {
  65. return mod_name + ".mod";
  66. }
  67. std::string cmFortranParser_s::SModName(std::string const& mod_name,
  68. std::string const& sub_name) const
  69. {
  70. std::string const& SModExt =
  71. this->Compiler.SModExt.empty() ? ".mod" : this->Compiler.SModExt;
  72. // An empty separator means that the compiler does not use a prefix.
  73. if (this->Compiler.SModSep.empty()) {
  74. return sub_name + SModExt;
  75. }
  76. return mod_name + this->Compiler.SModSep + sub_name + SModExt;
  77. }
  78. bool cmFortranParser_FilePush(cmFortranParser* parser, const char* fname)
  79. {
  80. // Open the new file and push it onto the stack. Save the old
  81. // buffer with it on the stack.
  82. if (FILE* file = cmsys::SystemTools::Fopen(fname, "rb")) {
  83. YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
  84. std::string dir = cmSystemTools::GetParentDirectory(fname);
  85. cmFortranFile f(file, current, dir);
  86. YY_BUFFER_STATE buffer =
  87. cmFortran_yy_create_buffer(nullptr, 16384, parser->Scanner);
  88. cmFortran_yy_switch_to_buffer(buffer, parser->Scanner);
  89. parser->FileStack.push(f);
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool cmFortranParser_FilePop(cmFortranParser* parser)
  95. {
  96. // Pop one file off the stack and close it. Switch the lexer back
  97. // to the next one on the stack.
  98. if (parser->FileStack.empty()) {
  99. return false;
  100. }
  101. cmFortranFile f = parser->FileStack.top();
  102. parser->FileStack.pop();
  103. fclose(f.File);
  104. YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
  105. cmFortran_yy_delete_buffer(current, parser->Scanner);
  106. cmFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
  107. return true;
  108. }
  109. int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
  110. size_t bufferSize)
  111. {
  112. // Read from the file on top of the stack. If the stack is empty,
  113. // the end of the translation unit has been reached.
  114. if (!parser->FileStack.empty()) {
  115. cmFortranFile& ff = parser->FileStack.top();
  116. FILE* file = ff.File;
  117. size_t n = fread(buffer, 1, bufferSize, file);
  118. if (n > 0) {
  119. ff.LastCharWasNewline = buffer[n - 1] == '\n';
  120. } else if (!ff.LastCharWasNewline) {
  121. // The file ended without a newline. Inject one so
  122. // that the file always ends in an end-of-statement.
  123. buffer[0] = '\n';
  124. n = 1;
  125. ff.LastCharWasNewline = true;
  126. }
  127. return static_cast<int>(n);
  128. }
  129. return 0;
  130. }
  131. void cmFortranParser_StringStart(cmFortranParser* parser)
  132. {
  133. parser->TokenString.clear();
  134. }
  135. const char* cmFortranParser_StringEnd(cmFortranParser* parser)
  136. {
  137. return parser->TokenString.c_str();
  138. }
  139. void cmFortranParser_StringAppend(cmFortranParser* parser, char c)
  140. {
  141. parser->TokenString += c;
  142. }
  143. void cmFortranParser_SetInInterface(cmFortranParser* parser, bool in)
  144. {
  145. if (parser->InPPFalseBranch) {
  146. return;
  147. }
  148. parser->InInterface = in;
  149. }
  150. bool cmFortranParser_GetInInterface(cmFortranParser* parser)
  151. {
  152. return parser->InInterface;
  153. }
  154. void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg)
  155. {
  156. parser->OldStartcond = arg;
  157. }
  158. int cmFortranParser_GetOldStartcond(cmFortranParser* parser)
  159. {
  160. return parser->OldStartcond;
  161. }
  162. void cmFortranParser_Error(cmFortranParser* parser, const char* msg)
  163. {
  164. parser->Error = msg ? msg : "unknown error";
  165. }
  166. void cmFortranParser_RuleUse(cmFortranParser* parser, const char* module_name)
  167. {
  168. if (parser->InPPFalseBranch) {
  169. return;
  170. }
  171. // syntax: "use module_name"
  172. // requires: "module_name.mod"
  173. std::string const& mod_name = cmSystemTools::LowerCase(module_name);
  174. parser->Info.Requires.insert(parser->ModName(mod_name));
  175. }
  176. void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
  177. const char* filename)
  178. {
  179. // This is a #line directive naming a file encountered during preprocessing.
  180. std::string included = filename;
  181. // Skip #line directives referencing non-files like
  182. // "<built-in>" or "<command-line>".
  183. if (included.empty() || included[0] == '<') {
  184. return;
  185. }
  186. // Fix windows file path separators since our lexer does not
  187. // process escape sequences in string literals.
  188. cmSystemTools::ReplaceString(included, "\\\\", "\\");
  189. cmSystemTools::ConvertToUnixSlashes(included);
  190. // Save the named file as included in the source.
  191. if (cmSystemTools::FileExists(included, true)) {
  192. parser->Info.Includes.insert(included);
  193. }
  194. }
  195. void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name)
  196. {
  197. if (parser->InPPFalseBranch) {
  198. return;
  199. }
  200. // If processing an include statement there must be an open file.
  201. assert(!parser->FileStack.empty());
  202. // Get the directory containing the source in which the include
  203. // statement appears. This is always the first search location for
  204. // Fortran include files.
  205. std::string dir = parser->FileStack.top().Directory;
  206. // Find the included file. If it cannot be found just ignore the
  207. // problem because either the source will not compile or the user
  208. // does not care about depending on this included source.
  209. std::string fullName;
  210. if (parser->FindIncludeFile(dir.c_str(), name, fullName)) {
  211. // Found the included file. Save it in the set of included files.
  212. parser->Info.Includes.insert(fullName);
  213. // Parse it immediately to translate the source inline.
  214. cmFortranParser_FilePush(parser, fullName.c_str());
  215. }
  216. }
  217. void cmFortranParser_RuleModule(cmFortranParser* parser,
  218. const char* module_name)
  219. {
  220. if (parser->InPPFalseBranch) {
  221. return;
  222. }
  223. if (!parser->InInterface) {
  224. // syntax: "module module_name"
  225. // provides: "module_name.mod"
  226. std::string const& mod_name = cmSystemTools::LowerCase(module_name);
  227. parser->Info.Provides.insert(parser->ModName(mod_name));
  228. }
  229. }
  230. void cmFortranParser_RuleSubmodule(cmFortranParser* parser,
  231. const char* module_name,
  232. const char* submodule_name)
  233. {
  234. if (parser->InPPFalseBranch) {
  235. return;
  236. }
  237. // syntax: "submodule (module_name) submodule_name"
  238. // requires: "module_name.mod"
  239. // provides: "module_name@submodule_name.smod"
  240. //
  241. // FIXME: Some compilers split the submodule part of a module into a
  242. // separate "module_name.smod" file. Whether it is generated or
  243. // not depends on conditions more subtle than we currently detect.
  244. // For now we depend directly on "module_name.mod".
  245. std::string const& mod_name = cmSystemTools::LowerCase(module_name);
  246. std::string const& sub_name = cmSystemTools::LowerCase(submodule_name);
  247. parser->Info.Requires.insert(parser->ModName(mod_name));
  248. parser->Info.Provides.insert(parser->SModName(mod_name, sub_name));
  249. }
  250. void cmFortranParser_RuleSubmoduleNested(cmFortranParser* parser,
  251. const char* module_name,
  252. const char* submodule_name,
  253. const char* nested_submodule_name)
  254. {
  255. if (parser->InPPFalseBranch) {
  256. return;
  257. }
  258. // syntax: "submodule (module_name:submodule_name) nested_submodule_name"
  259. // requires: "module_name@submodule_name.smod"
  260. // provides: "module_name@nested_submodule_name.smod"
  261. std::string const& mod_name = cmSystemTools::LowerCase(module_name);
  262. std::string const& sub_name = cmSystemTools::LowerCase(submodule_name);
  263. std::string const& nest_name =
  264. cmSystemTools::LowerCase(nested_submodule_name);
  265. parser->Info.Requires.insert(parser->SModName(mod_name, sub_name));
  266. parser->Info.Provides.insert(parser->SModName(mod_name, nest_name));
  267. }
  268. void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* macro)
  269. {
  270. if (!parser->InPPFalseBranch) {
  271. parser->PPDefinitions.insert(macro);
  272. }
  273. }
  274. void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* macro)
  275. {
  276. if (!parser->InPPFalseBranch) {
  277. std::set<std::string>::iterator match;
  278. match = parser->PPDefinitions.find(macro);
  279. if (match != parser->PPDefinitions.end()) {
  280. parser->PPDefinitions.erase(match);
  281. }
  282. }
  283. }
  284. void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* macro)
  285. {
  286. // A new PP branch has been opened
  287. parser->SkipToEnd.push(false);
  288. if (parser->InPPFalseBranch) {
  289. parser->InPPFalseBranch++;
  290. } else if (parser->PPDefinitions.find(macro) ==
  291. parser->PPDefinitions.end()) {
  292. parser->InPPFalseBranch = 1;
  293. } else {
  294. parser->SkipToEnd.top() = true;
  295. }
  296. }
  297. void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* macro)
  298. {
  299. // A new PP branch has been opened
  300. parser->SkipToEnd.push(false);
  301. if (parser->InPPFalseBranch) {
  302. parser->InPPFalseBranch++;
  303. } else if (parser->PPDefinitions.find(macro) !=
  304. parser->PPDefinitions.end()) {
  305. parser->InPPFalseBranch = 1;
  306. } else {
  307. // ignore other branches
  308. parser->SkipToEnd.top() = true;
  309. }
  310. }
  311. void cmFortranParser_RuleIf(cmFortranParser* parser)
  312. {
  313. /* Note: The current parser is _not_ able to get statements like
  314. * #if 0
  315. * #if 1
  316. * #if MYSMBOL
  317. * #if defined(MYSYMBOL)
  318. * #if defined(MYSYMBOL) && ...
  319. * right. The same for #elif. Thus in
  320. * #if SYMBOL_1
  321. * ..
  322. * #elif SYMBOL_2
  323. * ...
  324. * ...
  325. * #elif SYMBOL_N
  326. * ..
  327. * #else
  328. * ..
  329. * #endif
  330. * _all_ N+1 branches are considered. If you got something like this
  331. * #if defined(MYSYMBOL)
  332. * #if !defined(MYSYMBOL)
  333. * use
  334. * #ifdef MYSYMBOL
  335. * #ifndef MYSYMBOL
  336. * instead.
  337. */
  338. // A new PP branch has been opened
  339. // Never skip! See note above.
  340. parser->SkipToEnd.push(false);
  341. }
  342. void cmFortranParser_RuleElif(cmFortranParser* parser)
  343. {
  344. /* Note: There are parser limitations. See the note at
  345. * cmFortranParser_RuleIf(..)
  346. */
  347. // Always taken unless an #ifdef or #ifndef-branch has been taken
  348. // already. If the second condition isn't meet already
  349. // (parser->InPPFalseBranch == 0) correct it.
  350. if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top() &&
  351. !parser->InPPFalseBranch) {
  352. parser->InPPFalseBranch = 1;
  353. }
  354. }
  355. void cmFortranParser_RuleElse(cmFortranParser* parser)
  356. {
  357. // if the parent branch is false do nothing!
  358. if (parser->InPPFalseBranch > 1) {
  359. return;
  360. }
  361. // parser->InPPFalseBranch is either 0 or 1. We change it depending on
  362. // parser->SkipToEnd.top()
  363. if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top()) {
  364. parser->InPPFalseBranch = 1;
  365. } else {
  366. parser->InPPFalseBranch = 0;
  367. }
  368. }
  369. void cmFortranParser_RuleEndif(cmFortranParser* parser)
  370. {
  371. if (!parser->SkipToEnd.empty()) {
  372. parser->SkipToEnd.pop();
  373. }
  374. // #endif doesn't know if there was a "#else" in before, so it
  375. // always decreases InPPFalseBranch
  376. if (parser->InPPFalseBranch) {
  377. parser->InPPFalseBranch--;
  378. }
  379. }