cmFortranParserImpl.cxx 12 KB

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