cmFortranParserImpl.cxx 13 KB

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