1 //===- FrontendOptions.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Frontend/FrontendOptions.h"
10 #include "flang/Evaluate/expression.h"
11 
12 using namespace Fortran::frontend;
13 
14 bool Fortran::frontend::isFixedFormSuffix(llvm::StringRef suffix) {
15   // Note: Keep this list in-sync with flang/test/lit.cfg.py
16   return suffix == "f77" || suffix == "f" || suffix == "F" || suffix == "ff" ||
17       suffix == "for" || suffix == "FOR" || suffix == "fpp" || suffix == "FPP";
18 }
19 
20 bool Fortran::frontend::isFreeFormSuffix(llvm::StringRef suffix) {
21   // Note: Keep this list in-sync with flang/test/lit.cfg.py
22   // TODO: Add Cuda Fortan files (i.e. `*.cuf` and `*.CUF`).
23   return suffix == "f90" || suffix == "F90" || suffix == "ff90" ||
24       suffix == "f95" || suffix == "F95" || suffix == "ff95" ||
25       suffix == "f03" || suffix == "F03" || suffix == "f08" ||
26       suffix == "F08" || suffix == "f18" || suffix == "F18";
27 }
28 
29 bool Fortran::frontend::mustBePreprocessed(llvm::StringRef suffix) {
30   return suffix == "F" || suffix == "FOR" || suffix == "fpp" ||
31       suffix == "FPP" || suffix == "F90" || suffix == "F95" ||
32       suffix == "F03" || suffix == "F08" || suffix == "F18";
33 }
34 
35 // TODO: This is a copy of `asFortran` from f18.cpp and is added here for
36 // compatiblity. It doesn't really belong here, but I couldn't find a better
37 // place. We should decide whether to add it to the Evaluate or Parse/Unparse
38 // APIs or some dedicated utility library in the driver.
39 Fortran::parser::AnalyzedObjectsAsFortran
40 Fortran::frontend::getBasicAsFortran() {
41   return Fortran::parser::AnalyzedObjectsAsFortran{
42       [](llvm::raw_ostream &o, const Fortran::evaluate::GenericExprWrapper &x) {
43         if (x.v) {
44           x.v->AsFortran(o);
45         } else {
46           o << "(bad expression)";
47         }
48       },
49       [](llvm::raw_ostream &o,
50           const Fortran::evaluate::GenericAssignmentWrapper &x) {
51         if (x.v) {
52           x.v->AsFortran(o);
53         } else {
54           o << "(bad assignment)";
55         }
56       },
57       [](llvm::raw_ostream &o, const Fortran::evaluate::ProcedureRef &x) {
58         x.AsFortran(o << "CALL ");
59       },
60   };
61 }
62 
63 InputKind FrontendOptions::GetInputKindForExtension(llvm::StringRef extension) {
64   if (isFixedFormSuffix(extension) || isFreeFormSuffix(extension)) {
65     return Language::Fortran;
66   }
67   return Language::Unknown;
68 }
69