    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Distribution.Simple.CCompiler
    -- Copyright   :  2011, Dan Knapp
    --
    -- Maintainer  :  cabal-devel@haskell.org
    -- Portability :  portable
    --
    -- This simple package provides types and functions for interacting with
    -- C compilers.  Currently it's just a type enumerating extant C-like
    -- languages, which we call dialects.

    {-
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.

        * Redistributions in binary form must reproduce the above
          copyright notice, this list of conditions and the following
          disclaimer in the documentation and/or other materials provided
          with the distribution.

        * Neither the name of Isaac Jones nor the names of other
          contributors may be used to endorse or promote products derived
          from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

    module Distribution.Simple.CCompiler (
       CDialect(..),
       cSourceExtensions,
       cDialectFilenameExtension,
       filenameCDialect
      ) where

    import Prelude ()
    import Distribution.Compat.Prelude

    import System.FilePath
         ( takeExtension )

    -- | Represents a dialect of C.  The Monoid instance expresses backward
    --   compatibility, in the sense that 'mappend a b' is the least inclusive
    --   dialect which both 'a' and 'b' can be correctly interpreted as.
    data CDialect = C
                  | ObjectiveC
                  | CPlusPlus
                  | ObjectiveCPlusPlus
                  deriving (CDialect -> CDialect -> Bool
    (CDialect -> CDialect -> Bool)
    -> (CDialect -> CDialect -> Bool) -> Eq CDialect
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: CDialect -> CDialect -> Bool
    == :: CDialect -> CDialect -> Bool
    $c/= :: CDialect -> CDialect -> Bool
    /= :: CDialect -> CDialect -> Bool
    Eq, Int -> CDialect -> ShowS
    [CDialect] -> ShowS
    CDialect -> String
    (Int -> CDialect -> ShowS)
    -> (CDialect -> String) -> ([CDialect] -> ShowS) -> Show CDialect
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> CDialect -> ShowS
    showsPrec :: Int -> CDialect -> ShowS
    $cshow :: CDialect -> String
    show :: CDialect -> String
    $cshowList :: [CDialect] -> ShowS
    showList :: [CDialect] -> ShowS
    Show)

    instance Monoid CDialect where
      mempty :: CDialect
    mempty = CDialect
    C
      mappend :: CDialect -> CDialect -> CDialect
    mappend = CDialect -> CDialect -> CDialect
    forall a. Semigroup a => a -> a -> a
    (<>)

    instance Semigroup CDialect where
      CDialect
    C                  <> :: CDialect -> CDialect -> CDialect
    <> CDialect
    anything           = CDialect
    anything
      CDialect
    ObjectiveC         <> CDialect
    CPlusPlus          = CDialect
    ObjectiveCPlusPlus
      CDialect
    CPlusPlus          <> CDialect
    ObjectiveC         = CDialect
    ObjectiveCPlusPlus
      CDialect
    _                  <> CDialect
    ObjectiveCPlusPlus = CDialect
    ObjectiveCPlusPlus
      CDialect
    ObjectiveC         <> CDialect
    _                  = CDialect
    ObjectiveC
      CDialect
    CPlusPlus          <> CDialect
    _                  = CDialect
    CPlusPlus
      CDialect
    ObjectiveCPlusPlus <> CDialect
    _                  = CDialect
    ObjectiveCPlusPlus

    -- | A list of all file extensions which are recognized as possibly containing
    --   some dialect of C code.  Note that this list is only for source files,
    --   not for header files.
    cSourceExtensions :: [String]
    cSourceExtensions :: [String]
    cSourceExtensions = [String
    "c", String
    "i", String
    "ii", String
    "m", String
    "mi", String
    "mm", String
    "M", String
    "mii", String
    "cc", String
    "cp",
                         String
    "cxx", String
    "cpp", String
    "CPP", String
    "c++", String
    "C"]

    -- | Takes a dialect of C and whether code is intended to be passed through
    --   the preprocessor, and returns a filename extension for containing that
    --   code.
    cDialectFilenameExtension :: CDialect -> Bool -> String
    cDialectFilenameExtension :: CDialect -> Bool -> String
    cDialectFilenameExtension CDialect
    C Bool
    True  = String
    "c"
    cDialectFilenameExtension CDialect
    C Bool
    False = String
    "i"
    cDialectFilenameExtension CDialect
    ObjectiveC Bool
    True  = String
    "m"
    cDialectFilenameExtension CDialect
    ObjectiveC Bool
    False = String
    "mi"
    cDialectFilenameExtension CDialect
    CPlusPlus Bool
    True   = String
    "cpp"
    cDialectFilenameExtension CDialect
    CPlusPlus Bool
    False  = String
    "ii"
    cDialectFilenameExtension CDialect
    ObjectiveCPlusPlus Bool
    True  = String
    "mm"
    cDialectFilenameExtension CDialect
    ObjectiveCPlusPlus Bool
    False = String
    "mii"

    -- | Infers from a filename's extension the dialect of C which it contains,
    --   and whether it is intended to be passed through the preprocessor.
    filenameCDialect :: String -> Maybe (CDialect, Bool)
    filenameCDialect :: String -> Maybe (CDialect, Bool)
    filenameCDialect String
    filename = do
      String
    extension <- case ShowS
    takeExtension String
    filename of
                     Char
    '.':String
    ext -> String -> Maybe String
    forall a. a -> Maybe a
    Just String
    ext
                     String
    _       -> Maybe String
    forall a. Maybe a
    Nothing
      case String
    extension of
        String
    "c"   -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    C, Bool
    True)
        String
    "i"   -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    C, Bool
    False)
        String
    "ii"  -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    False)
        String
    "m"   -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    ObjectiveC, Bool
    True)
        String
    "mi"  -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    ObjectiveC, Bool
    False)
        String
    "mm"  -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    ObjectiveCPlusPlus, Bool
    True)
        String
    "M"   -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    ObjectiveCPlusPlus, Bool
    True)
        String
    "mii" -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    ObjectiveCPlusPlus, Bool
    False)
        String
    "cc"  -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    "cp"  -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    "cxx" -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    "cpp" -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    "CPP" -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    "c++" -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    "C"   -> (CDialect, Bool) -> Maybe (CDialect, Bool)
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CDialect
    CPlusPlus, Bool
    True)
        String
    _     -> Maybe (CDialect, Bool)
    forall a. Maybe a
    Nothing