{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}

module Demangler
  (
    Context
  , newDemangling
  , Demangled
  , Result
  , demangle
  , demangle1
  )
where

import           Control.Applicative
import           Control.Lens ( (&), (^.), view, (.~), (||~) )
import           Control.Monad
import           Data.Char
import           Data.Either ( isRight )
import           Data.List.NonEmpty ( NonEmpty((:|)) )
import qualified Data.List.NonEmpty as NEL
import           Data.Maybe
import           Data.Semigroup ( sconcat )
import           Data.Text ( Text )
import qualified Data.Text as T
import           Text.Sayable

import           Demangler.Context
import           Demangler.Engine
import           Demangler.PPrint ()
import           Demangler.Structure
import           Demangler.Substitution

#ifdef MIN_VERSION_panic
-- The debug flag is enabled in the cabal file
import           Debug.Trace
#endif


----------------------------------------------------------------------


-- | Demangle a single entry.  If there are multiple entries to be demangled, use
-- 'demangle' for efficient batching and memory reduction.

demangle1 :: Text -> Result
demangle1 :: Text -> Result
demangle1 Text
s = Text -> Context -> Result
demangle Text
s Context
newDemangling

-- | Demangle an input string, given a Context for demangling.
--
-- The signature of this function makes it ideal for State evaluation.

demangle :: Text -> Context -> Result
demangle :: Text -> Context -> Result
demangle Text
s Context
c =
  let seed :: (Text,
 ((),
  (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
seed = (Text
s, ((), (Context
c, (forall a. Monoid a => a
mempty, (forall a. Monoid a => a
mempty, Bool
False, Bool
False))))) in
  case forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ((forall a b. (a -> b) -> a -> b
$ (Text,
 ((),
  (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
seed) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ (Text,
 ((),
  (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
-> Maybe (NextArg Demangled)
mangled, AnyNext Demangled
plain ]) of
                 Just NextArg Demangled
r -> (NextArg Demangled
r forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal, NextArg Demangled
r forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Context
nContext)
                 Maybe (NextArg Demangled)
_ -> let (Int
i,Context
c') = Text -> Context -> (Int, Context)
contextFindOrAdd Text
s Context
c
                      in (Int -> Demangled
Original Int
i, Context
c')


--------------------
-- Mangled name parsing of various elements

mangled :: Next () Demangled
mangled :: (Text,
 ((),
  (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
-> Maybe (NextArg Demangled)
mangled = forall a. Text -> Next a a
match Text
"_Z" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Encoding
encoding forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=>
          forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"." forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NextArg Encoding -> Maybe (NextArg Demangled)
vendorExtension forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Next a a
end_of_input
                , forall a. Next a a
end_of_input forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Encoding -> Demangled
Encoded
                ]

vendorExtension :: Next Encoding Demangled
vendorExtension :: NextArg Encoding -> Maybe (NextArg Demangled)
vendorExtension NextArg Encoding
x =
  let (Int
i, Context
c') = Text -> Context -> (Int, Context)
contextFindOrAdd (NextArg Encoding
x forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Text
nInp) (NextArg Encoding
x forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Context
nContext)
  in forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ NextArg Encoding
x
     forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Text
nInp forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
""
     forall a b. a -> (a -> b) -> b
& forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall s t a b. ASetter s t a b -> b -> s -> t
.~ Encoding -> Int -> Demangled
VendorExtended (NextArg Encoding
x forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) Int
i
     forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Context
nContext forall s t a b. ASetter s t a b -> b -> s -> t
.~ Context
c'

plain :: AnyNext Demangled
plain :: AnyNext Demangled
plain NextArg a
x =
  let (Int
o, Context
c') = Text -> Context -> (Int, Context)
contextFindOrAdd (NextArg a
x forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Text
nInp) (NextArg a
x forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Context
nContext)
  in forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ NextArg a
x
     forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Text
nInp forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
""
     forall a b. a -> (a -> b) -> b
& forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int -> Demangled
Original Int
o
     forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Context
nContext forall s t a b. ASetter s t a b -> b -> s -> t
.~ Context
c'

encoding :: AnyNext Encoding
encoding :: AnyNext Encoding
encoding =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext FunctionName
function_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Bool -> Next FunctionName Encoding
bare_function_type Bool
False
        , forall a. Text -> Next a a
match Text
"L" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext FunctionName
function_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Bool -> Next FunctionName Encoding
bare_function_type Bool
True
        , AnyNext Encoding
data_name
        , AnyNext Encoding
const_struct_data
        , AnyNext Encoding
special_name
       ]

function_name :: AnyNext FunctionName
function_name :: AnyNext FunctionName
function_name = forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Name -> FunctionName
FunctionName forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< AnyNext Name
name

data_name :: AnyNext Encoding
data_name :: AnyNext Encoding
data_name = AnyNext Name
name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Name -> Encoding
EncData

const_struct_data :: AnyNext Encoding
const_struct_data :: AnyNext Encoding
const_struct_data = forall a. Text -> Next a a
match Text
"L" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext UnqualifiedName
unqualified_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap UnqualifiedName -> Encoding
EncConstStructData

special_name :: AnyNext Encoding
special_name :: AnyNext Encoding
special_name =
  forall a. Text -> Next a a
match Text
"T"
  forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"A" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext TemplateArg
template_arg forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap TemplateArg -> SpecialName
TemplateParameterObj
            , forall a. Text -> Next a a
match Text
"V" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> SpecialName
VirtualTable
            , forall a. Text -> Next a a
match Text
"T" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> SpecialName
VTT
            , forall a. Text -> Next a a
match Text
"I" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> SpecialName
TypeInfo
            , forall a. Text -> Next a a
match Text
"S" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> SpecialName
TypeInfoName
            , forall a. Text -> Next a a
match Text
"c" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. String -> Next a b
tbd String
"covariant return thunk to"
            , forall a. Text -> Next a a
match Text
"C" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. String -> Next a b
tbd String
"ctor vtable special name"
            , forall a. Text -> Next a a
match Text
"W" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. String -> Next a b
tbd String
"thread-local wrapper routine for"
            , forall a. Text -> Next a a
match Text
"H" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. String -> Next a b
tbd String
"thread-local initialization routine for"
            , AnyNext CallOffset
call_offset forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext Encoding
encoding forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry CallOffset -> Encoding -> SpecialName
Thunk)
            ]
  forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap SpecialName -> Encoding
EncSpecial

call_offset :: AnyNext CallOffset
call_offset :: AnyNext CallOffset
call_offset = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"h" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
int_number forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_"
                      forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Int -> CallOffset
NonVirtualOffset
                    , forall a. Text -> Next a a
match Text
"v" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
int_number forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_" forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext Int
digits_num
                      forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_"
                      forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> CallOffset
VirtualOffset)
                    ]


int_number :: AnyNext Int
int_number :: AnyNext Int
int_number = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"n" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap ( (-Int
1) forall a. Num a => a -> a -> a
* )
                   , AnyNext Int
digits_num
                   ]

name :: AnyNext Name
name :: AnyNext Name
name = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext NestedName
nested_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap NestedName -> Name
NameNested
             , AnyNext Name
unscoped_template_name forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext (NonEmpty TemplateArg)
template_args
               forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Name -> NonEmpty TemplateArg -> Name
UnscopedTemplateName)
             , AnyNext Name
local_name
             , AnyNext Name
unscoped_name
             ]

nested_name :: AnyNext NestedName
nested_name :: AnyNext NestedName
nested_name = forall a. Text -> Next a a
match Text
"N"
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext [CVQualifier]
cv_qualifiers
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. Next a b -> Next a (a, Maybe b)
optional' AnyNext RefQualifier
ref_qualifier
              -- here: (Maybe [CVQualifier], Maybe RefQualifier)
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ NextArg ([CVQualifier], Maybe RefQualifier)
-> Maybe (NextArg NestedName)
form1 forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E"
                        , NextArg ([CVQualifier], Maybe RefQualifier)
-> Maybe (NextArg NestedName)
form2 forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E"
                        ]
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Next a a
dropLastSubst
  where form1 :: NextArg ([CVQualifier], Maybe RefQualifier)
-> Maybe (NextArg NestedName)
form1 NextArg ([CVQualifier], Maybe RefQualifier)
i = do NextArg Prefix
p <- AnyNext Prefix
prefix NextArg ([CVQualifier], Maybe RefQualifier)
i
                     -- Parse ambiguity here:
                     --
                     --   nested-name ::= N ... <prefix> <unqualified-name> E
                     --   prefix ::= <unqualified-name>
                     --            | <prefix> <unqualified-name>
                     --            | ... others ...
                     --
                     -- Thus, in order to match here, <prefix> *must* have ended
                     -- in an <unqualified-name> match, and that match should be
                     -- removed from the prefix 'p' and used as the final
                     -- <nested-name> element.
                     (Prefix
rmnpfx, Either UnqualifiedName (NonEmpty TemplateArg)
ent) <- Prefix
-> Maybe (Prefix, Either UnqualifiedName (NonEmpty TemplateArg))
prefixInitLast forall a b. (a -> b) -> a -> b
$ NextArg Prefix
p forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                     case Either UnqualifiedName (NonEmpty TemplateArg)
ent of
                       Right NonEmpty TemplateArg
_ -> forall a. Maybe a
Nothing
                       Left UnqualifiedName
uq ->
                         case Prefix
rmnpfx of
                           Prefix
EmptyPrefix -> forall a. Maybe a
Nothing
                           Prefix
pfx -> let ([CVQualifier]
cvq, Maybe RefQualifier
mb'refQual) = NextArg ([CVQualifier], Maybe RefQualifier)
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                                  in forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg Prefix
p (Prefix
-> UnqualifiedName
-> [CVQualifier]
-> Maybe RefQualifier
-> NestedName
NestedName Prefix
pfx UnqualifiedName
uq [CVQualifier]
cvq Maybe RefQualifier
mb'refQual)
        form2 :: NextArg ([CVQualifier], Maybe RefQualifier)
-> Maybe (NextArg NestedName)
form2 NextArg ([CVQualifier], Maybe RefQualifier)
i = do NextArg (TemplatePrefix, Maybe (NonEmpty TemplateArg))
pa <- AnyNext (TemplatePrefix, Maybe (NonEmpty TemplateArg))
template_prefix_and_args NextArg ([CVQualifier], Maybe RefQualifier)
i
                     let (TemplatePrefix
p,Maybe (NonEmpty TemplateArg)
mba) = NextArg (TemplatePrefix, Maybe (NonEmpty TemplateArg))
pa forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                     NonEmpty TemplateArg
a <- Maybe (NonEmpty TemplateArg)
mba
                     let ([CVQualifier]
cvq, Maybe RefQualifier
mb'refQual) = NextArg ([CVQualifier], Maybe RefQualifier)
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                     forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg (TemplatePrefix, Maybe (NonEmpty TemplateArg))
pa forall a b. (a -> b) -> a -> b
$ TemplatePrefix
-> NonEmpty TemplateArg
-> [CVQualifier]
-> Maybe RefQualifier
-> NestedName
NestedTemplateName TemplatePrefix
p NonEmpty TemplateArg
a [CVQualifier]
cvq Maybe RefQualifier
mb'refQual

unscoped_name :: AnyNext Name
unscoped_name :: AnyNext Name
unscoped_name =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext UnqualifiedName
unqualified_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Bool -> UnqualifiedName -> Name
UnscopedName Bool
False)
        , forall a. Text -> Next a a
match Text
"St" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext UnqualifiedName
unqualified_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Bool -> UnqualifiedName -> Name
UnscopedName Bool
True)
        ]

unscoped_template_name :: AnyNext Name
unscoped_template_name :: AnyNext Name
unscoped_template_name NextArg a
i =
  (AnyNext Name
unscoped_name NextArg a
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next Name Name
canSubstUnscopedTemplateName)
  forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (AnyNext Substitution'
substitution NextArg a
i
        forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next Substitution UnqualifiedName
-> Next Substitution' UnqualifiedName
substituteUnqualifiedName (forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Substitution -> UnqualifiedName
StdSubst)
        forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Bool -> UnqualifiedName -> Name
UnscopedName Bool
False)
      )


local_name :: AnyNext Name
local_name :: AnyNext Name
local_name NextArg a
i = do NextArg Int
f <- forall a. Text -> Next a a
match Text
"Z" NextArg a
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AnyNext Int
function_encoding
                  NextArg Int
c <- forall a. Text -> Next a a
match Text
"E" NextArg Int
f
                  forall {a}. NextArg Int -> NextArg a -> Maybe (NextArg Name)
entity NextArg Int
f NextArg Int
c forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> NextArg Int -> NextArg Int -> Maybe (NextArg Name)
stringlit NextArg Int
f NextArg Int
c
  where
    entity :: NextArg Int -> NextArg a -> Maybe (NextArg Name)
entity NextArg Int
f NextArg a
c = do NextArg Int
e <- AnyNext Int
entity_name NextArg a
c
                    NextArg Int -> NextArg Int -> Maybe (NextArg Name)
discriminated NextArg Int
f NextArg Int
e
                      forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg Int
e (Int -> Int -> Maybe Int -> Name
LocalName (NextArg Int
f forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg Int
e forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) forall a. Maybe a
Nothing)
    stringlit :: NextArg Int -> NextArg Int -> Maybe (NextArg Name)
stringlit NextArg Int
f NextArg Int
c = do NextArg Int
s <- forall a. Text -> Next a a
match Text
"s" NextArg Int
c
                       Maybe (NextArg Int)
d <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (AnyNext Int
discriminator NextArg Int
s)
                       forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret (forall a. a -> Maybe a -> a
fromMaybe NextArg Int
s Maybe (NextArg Int)
d) forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Name
StringLitName (NextArg Int
f forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NextArg Int)
d)
    discriminated :: NextArg Int -> NextArg Int -> Maybe (NextArg Name)
discriminated NextArg Int
f NextArg Int
e = do NextArg Int
d <- AnyNext Int
discriminator NextArg Int
e
                           forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg Int
d forall a b. (a -> b) -> a -> b
$ Int -> Int -> Maybe Int -> Name
LocalName (NextArg Int
f forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg Int
e forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ NextArg Int
d forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)


-- | Parse any CV qualifiers; always succeeds but might return an empty array.
-- Also note that while it is an array, each entry can appear at most once.
cv_qualifiers :: AnyNext [CVQualifier]
cv_qualifiers :: AnyNext [CVQualifier]
cv_qualifiers =
  let ifPresent :: a -> NextArg ([a], Maybe a) -> f (NextArg [a])
ifPresent a
v NextArg ([a], Maybe a)
i = forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (\([a]
a,Maybe a
p) -> if forall a. Maybe a -> Bool
isJust Maybe a
p then a
vforall a. a -> [a] -> [a]
:[a]
a else [a]
a) NextArg ([a], Maybe a)
i
  in forall b a. b -> Next a b
insert []
     forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. Next a b -> Next a (a, Maybe b)
optional' (forall a. Text -> Next a a
match Text
"K") forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {f :: * -> *} {a} {a}.
Applicative f =>
a -> NextArg ([a], Maybe a) -> f (NextArg [a])
ifPresent CVQualifier
Const_
     forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. Next a b -> Next a (a, Maybe b)
optional' (forall a. Text -> Next a a
match Text
"V") forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {f :: * -> *} {a} {a}.
Applicative f =>
a -> NextArg ([a], Maybe a) -> f (NextArg [a])
ifPresent CVQualifier
Volatile
     forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. Next a b -> Next a (a, Maybe b)
optional' (forall a. Text -> Next a a
match Text
"r") forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall {f :: * -> *} {a} {a}.
Applicative f =>
a -> NextArg ([a], Maybe a) -> f (NextArg [a])
ifPresent CVQualifier
Restrict

ref_qualifier :: AnyNext RefQualifier
ref_qualifier :: AnyNext RefQualifier
ref_qualifier = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"&&" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b. a -> b -> a
const RefQualifier
RefRef)
                      , forall a. Text -> Next a a
match Text
"&" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b. a -> b -> a
const RefQualifier
Ref)
                      ]

-- | Parse prefix.  This is a bit tricky though.  The BNF specifies:
--
--     prefix ::= <unqualified-name>
--            | <prefix> <unqualified-name>
--            | ... others ...
--            | substitution
--
-- but it cannot be expressed directly that way (it would either stop at the
-- *first* unqualified name, or if the first two were reverse, <prefix> would be
-- infinitely recursive because it would recurse without consuming input.  Note
-- however that a recursive prefix *always* terminates with an
-- <unqualified_name>, and can only recurse on that terminator, so once an
-- <unqualified_name> is parsed, the only remaining possibility is *one* more
-- <unqualified_name> entries.
--
-- In addition, the BNF is incorrect, both from the itanium-abi website, and also
-- in the LLVM code comments, which is slightly different, but still incorrect.
-- The BNF for a prefix indicates recursion can only happen before an unqualified
-- name, which precludes matching something like NS_6vectorIfE3fooE
-- ("matrix::Vector<float>::foo").  The LLVM code itself indicates that the
-- prefix is generally recursive, although a template_param, a decltype, or a
-- substitution may not be preceded by anything else, and template_args cannot be
-- immediately adjacent and must not be the starting element.

prefix :: AnyNext Prefix
prefix :: AnyNext Prefix
prefix NextArg a
i = (AnyNext Prefix
prefix'i NextArg a
i)
            forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (AnyNext Substitution'
substitution NextArg a
i
                 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next Substitution Prefix -> Next Substitution' Prefix
substitutePrefix Next Substitution Prefix
substitutionPrefix
                 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Prefix -> PrefixR -> Prefix
extendPrefix forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next (PrefixR -> Prefix) Prefix
prefix'r2
                           , forall (f :: * -> *) a. Applicative f => a -> f a
pure
                           ]
                )
  where
    prefix'i :: AnyNext Prefix
    prefix'i :: AnyNext Prefix
prefix'i =
      forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext TemplateArg
template_param
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap TemplateArg -> PrefixR -> Prefix
PrefixTemplateParam
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next (PrefixR -> Prefix) Prefix
prefix'r2
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NextArg Prefix -> Maybe (NextArg Prefix)
canSubstPrefix
            , AnyNext ()
decltype
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap () -> PrefixR -> Prefix
PrefixDeclType
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next (PrefixR -> Prefix) Prefix
prefix'r2
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NextArg Prefix -> Maybe (NextArg Prefix)
canSubstPrefix
            , forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' PrefixR -> Prefix
Prefix forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next (PrefixR -> Prefix) Prefix
prefix'r2
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \NextArg Prefix
p2 -> case NextArg Prefix
p2 forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal of
                           Prefix
EmptyPrefix -> forall a. Maybe a
Nothing
                           Prefix
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure NextArg Prefix
p2
            ]
    prefix'r2 :: Next (PrefixR -> Prefix) Prefix
    prefix'r2 :: Next (PrefixR -> Prefix) Prefix
prefix'r2 NextArg (PrefixR -> Prefix)
accum =
      forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [ -- Note that two sets of template_args will not occur
             -- back-to-back... at least for C/C++.  There are two dispositions
             -- that could be taken here:
             --
             -- 1. Ignore it if it happens (we didn't generate the mangled form)
             -- 2. Treat it as a demangling parse error.
             --
             -- The following require statements implements disposition 2;
             -- removing it would switch to disposition 1.
             Bool -> Maybe ()
require (Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ Prefix -> Bool
last_is_template_args (NextArg (PrefixR -> Prefix)
accum forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall a b. (a -> b) -> a -> b
$ PrefixR
PrefixEnd))
             forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> AnyNext (NonEmpty TemplateArg)
template_args NextArg (PrefixR -> Prefix)
accum
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (\NonEmpty TemplateArg
ta PrefixR
sp -> NextArg (PrefixR -> Prefix)
accum forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall a b. (a -> b) -> a -> b
$ NonEmpty TemplateArg -> PrefixR -> PrefixR
PrefixTemplateArgs NonEmpty TemplateArg
ta PrefixR
sp)
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next (PrefixR -> Prefix) (PrefixR -> Prefix)
canSubstAccumPrefix
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next (PrefixR -> Prefix) Prefix
prefix'r2
           , AnyNext UnqualifiedName
unqualified_name NextArg (PrefixR -> Prefix)
accum
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (\UnqualifiedName
uqn PrefixR
sp -> NextArg (PrefixR -> Prefix)
accum forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall a b. (a -> b) -> a -> b
$ UnqualifiedName -> PrefixR -> PrefixR
PrefixUQName UnqualifiedName
uqn PrefixR
sp)
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next (PrefixR -> Prefix) (PrefixR -> Prefix)
canSubstAccumPrefix
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next (PrefixR -> Prefix) Prefix
prefix'r2
           , AnyNext Substitution'
substitution NextArg (PrefixR -> Prefix)
accum
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next Substitution PrefixR -> Next Substitution' PrefixR
substitutePrefixR Next Substitution PrefixR
substitutionPrefixR
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (\PrefixR
subs -> Prefix -> PrefixR -> Prefix
extendPrefix ((NextArg (PrefixR -> Prefix)
accum forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) PrefixR
subs))
             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next (PrefixR -> Prefix) Prefix
prefix'r2
           , forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b. (a -> b) -> a -> b
$ PrefixR
PrefixEnd) NextArg (PrefixR -> Prefix)
accum
           ]
    canSubstAccumPrefix :: Next (PrefixR -> Prefix) (PrefixR -> Prefix)
    canSubstAccumPrefix :: Next (PrefixR -> Prefix) (PrefixR -> Prefix)
canSubstAccumPrefix NextArg (PrefixR -> Prefix)
sp = forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b. (a -> b) -> a -> b
$ PrefixR
PrefixEnd) NextArg (PrefixR -> Prefix)
sp
                             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NextArg Prefix -> Maybe (NextArg Prefix)
canSubstPrefix
                             forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' (NextArg (PrefixR -> Prefix)
sp forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)
    last_is_template_args :: Prefix -> Bool
last_is_template_args = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (forall a b. Either a b -> Bool
isRight forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Prefix
-> Maybe (Prefix, Either UnqualifiedName (NonEmpty TemplateArg))
prefixInitLast


substitutionPrefix :: Next Substitution Prefix
substitutionPrefix :: Next Substitution Prefix
substitutionPrefix = forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (PrefixR -> Prefix
Prefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. (a -> b) -> a -> b
$ PrefixR
PrefixEnd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnqualifiedName -> PrefixR -> PrefixR
PrefixUQName forall b c a. (b -> c) -> (a -> b) -> a -> c
. Substitution -> UnqualifiedName
StdSubst)

substitutionPrefixR :: Next Substitution PrefixR
substitutionPrefixR :: Next Substitution PrefixR
substitutionPrefixR = forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap ((forall a b. (a -> b) -> a -> b
$ PrefixR
PrefixEnd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnqualifiedName -> PrefixR -> PrefixR
PrefixUQName forall b c a. (b -> c) -> (a -> b) -> a -> c
. Substitution -> UnqualifiedName
StdSubst)

decltype :: AnyNext DeclType
decltype :: AnyNext ()
decltype = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"Dt" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Expression
expression forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. String -> Next a b
tbd String
"decltype1"
                 , forall a. Text -> Next a a
match Text
"DT" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Expression
expression forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. String -> Next a b
tbd String
"decltype2"
                 ]

closure_prefix :: AnyNext ClosurePrefix
closure_prefix :: AnyNext ()
closure_prefix = forall a b. String -> Next a b
tbd String
"closure prefix"

unqualified_name :: AnyNext UnqualifiedName
unqualified_name :: AnyNext UnqualifiedName
unqualified_name =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ -- (see parseUnqualifiedName in LLVM ItaniumDemangle.h)
          forall a. Next () a -> Next () [a]
many' AnyNext ModuleName
module_name forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
NextArg a
-> (Text,
    ((),
     (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
rdiscard forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"L" forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext UnqualifiedName
base_uqn
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [ModuleName] -> UnqualifiedName -> UnqualifiedName
ModuleNamed)
        , AnyNext UnqualifiedName
base_uqn
        ]


base_uqn :: AnyNext UnqualifiedName
base_uqn :: AnyNext UnqualifiedName
base_uqn = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ \NextArg a
i -> do NextArg Operator
op <- AnyNext Operator
operator_name NextArg a
i
                            NextArg [ABI_Tag]
at <- forall a. Next () a -> Next () [a]
many' AnyNext ABI_Tag
abi_tag forall a b. (a -> b) -> a -> b
$ forall a.
NextArg a
-> (Text,
    ((),
     (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
rdiscard NextArg Operator
op
                            forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg [ABI_Tag]
at forall a b. (a -> b) -> a -> b
$ Operator -> [ABI_Tag] -> UnqualifiedName
OperatorName (NextArg Operator
op forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg [ABI_Tag]
at forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)
                 , AnyNext CtorDtor
ctor_dtor_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap CtorDtor -> UnqualifiedName
CtorDtorName
                 , AnyNext SourceName
source_name forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> forall a. Next () a -> Next () [a]
many' AnyNext ABI_Tag
abi_tag forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
NextArg a
-> (Text,
    ((),
     (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
rdiscard
                   forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry SourceName -> [ABI_Tag] -> UnqualifiedName
SourceName)
                 , AnyNext UnqualifiedName
unnamed_type_name
                   -- , match "DC" i >>= some source_name >>= match "E"
                 ]

module_name :: AnyNext ModuleName
module_name :: AnyNext ModuleName
module_name = forall a. Text -> Next a a
match Text
"W"
              forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"P" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext SourceName
source_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Bool -> SourceName -> ModuleName
ModuleName Bool
True)
                        , AnyNext SourceName
source_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Bool -> SourceName -> ModuleName
ModuleName Bool
False)
                        ]

operator_name :: AnyNext Operator
operator_name :: AnyNext Operator
operator_name =
  let opMatch :: (b, (Text, b)) -> NextArg a -> Maybe (NextArg b)
opMatch (b
o,(Text
t,b
_)) = forall a. Text -> Next a a
match Text
t forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b. a -> b -> a
const b
o)
  in forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' ((forall {b} {b} {a}.
(b, (Text, b)) -> NextArg a -> Maybe (NextArg b)
opMatch forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Operator, (Text, Text))]
opTable)
             forall a. Semigroup a => a -> a -> a
<> [ forall a. Text -> Next a a
match Text
"cv" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> Operator
OpCast
                , forall a. Text -> Next a a
match Text
"li" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext SourceName
source_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap SourceName -> Operator
OpString
                , forall a. Text -> Next a a
match Text
"v" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
single_digit_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=>
                  \NextArg Int
d -> do NextArg SourceName
sn <- AnyNext SourceName
source_name NextArg Int
d
                           forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Natural -> SourceName -> Operator
OpVendor (forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ NextArg Int
d forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)) NextArg SourceName
sn
                ]
           )

abi_tag :: AnyNext ABI_Tag
abi_tag :: AnyNext ABI_Tag
abi_tag = forall a. Text -> Next a a
match Text
"B" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext SourceName
source_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap SourceName -> ABI_Tag
ABITag

ctor_dtor_name :: AnyNext CtorDtor
ctor_dtor_name :: AnyNext CtorDtor
ctor_dtor_name = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"C1" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' CtorDtor
CompleteCtor
                       , forall a. Text -> Next a a
match Text
"C2" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' CtorDtor
BaseCtor
                       , forall a. Text -> Next a a
match Text
"C3" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' CtorDtor
CompleteAllocatingCtor
                       , forall a. Text -> Next a a
match Text
"CI1" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> CtorDtor
CompleteInheritingCtor
                       , forall a. Text -> Next a a
match Text
"CI2" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> CtorDtor
BaseInheritingCtor
                       , forall a. Text -> Next a a
match Text
"D0" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' CtorDtor
DeletingDtor
                       , forall a. Text -> Next a a
match Text
"D1" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' CtorDtor
CompleteDtor
                       , forall a. Text -> Next a a
match Text
"D2" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' CtorDtor
BaseDtor
                       ]

source_name :: AnyNext SourceName
source_name :: AnyNext SourceName
source_name = AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next Int SourceName
identifier

identifier :: Next Int SourceName
identifier :: Next Int SourceName
identifier NextArg Int
i =
  let identChar :: Char -> Bool
identChar Char
x = Char -> Bool
isAlphaNum Char
x Bool -> Bool -> Bool
|| Char
x forall a. Eq a => a -> a -> Bool
== Char
'_'
      (Text
nm, Text
ri) = Int -> Text -> (Text, Text)
T.splitAt (NextArg Int
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg Int
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Text
nInp)
  in do Bool -> Maybe ()
require ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
identChar Text
nm)
        let (Int
idnt, Context
c') = Text -> Context -> (Int, Context)
contextFindOrAdd Text
nm (NextArg Int
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Context
nContext)
        forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ NextArg Int
i
          forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Text
nInp forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
ri
          forall a b. a -> (a -> b) -> b
& forall a b. Lens (NextArg a) (NextArg b) a b
nVal forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int -> SourceName
SrcName Int
idnt
          forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Context
nContext forall s t a b. ASetter s t a b -> b -> s -> t
.~ Context
c'


unnamed_type_name :: AnyNext UnqualifiedName
unnamed_type_name :: AnyNext UnqualifiedName
unnamed_type_name = forall a b. String -> Next a b
tbd String
"unnamed_type_name"

-- | Parse the function argument (and return) types for a function.
--
-- At this point, all template argments that can be substituted have been made,
-- so any template arguments occurring in the arguments are ignored as T[n]_
-- replacements.

bare_function_type :: Bool -> Next FunctionName Encoding
bare_function_type :: Bool -> Next FunctionName Encoding
bare_function_type Bool
isStatic NextArg FunctionName
i =
  let constr :: FunctionName -> Maybe Type_ -> NonEmpty Type_ -> Encoding
constr = if Bool
isStatic then FunctionName -> Maybe Type_ -> NonEmpty Type_ -> Encoding
EncStaticFunc else FunctionName -> Maybe Type_ -> NonEmpty Type_ -> Encoding
EncFunc in
  do NextArg (NonEmpty Type_)
tys <- AnyNext (NonEmpty Type_)
types_ forall a b. (a -> b) -> a -> b
$ NextArg FunctionName
i forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Bool
nTmplSubsLock forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
True
     -- Determine if the tys includes a function return type.  The rules are:
     --
     --  1. Template functions have return types encoded, with exceptions below.
     --  2. Function types not appearing as part of a function name mangling
     --     (e.g. parameters, pointer types, etc.) have return type encoded, with
     --     exceptions below.
     --  3. Non-template function names do not have return types encoded
     --
     -- Exceptions (for which return type is never included):
     --
     --  1. Constructors
     --  2. Destructors
     --  3. Conversion operator functions (e.g. operator int(..) )
     --
     let withRetType :: Maybe (NextArg Encoding)
withRetType = let (Type_
rty, Maybe (NonEmpty Type_)
argtys) = forall a. NonEmpty a -> (a, Maybe (NonEmpty a))
NEL.uncons forall a b. (a -> b) -> a -> b
$ NextArg (NonEmpty Type_)
tys forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                       in case Maybe (NonEmpty Type_)
argtys of
                            Just NonEmpty Type_
argtys' ->
                              forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg (NonEmpty Type_)
tys (FunctionName -> Maybe Type_ -> NonEmpty Type_ -> Encoding
constr (NextArg FunctionName
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (forall a. a -> Maybe a
Just Type_
rty) NonEmpty Type_
argtys')
                            Maybe (NonEmpty Type_)
Nothing ->
                              forall a b. a -> String -> [String] -> Maybe b
cannot Demangler
Demangler String
"bare_function_type.withRetType"
                              [ String
"Function with rtype and no argtypes: "
                              , forall (saytag :: Symbol) a. Sayable saytag a => a -> String
sez @"error" (forall a. a -> Context -> WithContext a
WC (NextArg FunctionName
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg FunctionName
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Context
nContext))
                              ]
     let noRetType :: Maybe (NextArg Encoding)
noRetType = forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (FunctionName -> Maybe Type_ -> NonEmpty Type_ -> Encoding
constr (NextArg FunctionName
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) forall a. Maybe a
Nothing) NextArg (NonEmpty Type_)
tys
     case NextArg FunctionName
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal of
       FunctionName
         (UnscopedTemplateName
           (UnscopedName Bool
_ (OperatorName (OpCast {}) [ABI_Tag]
_)) NonEmpty TemplateArg
_) -> Maybe (NextArg Encoding)
noRetType
       FunctionName (UnscopedTemplateName {}) -> Maybe (NextArg Encoding)
withRetType
       FunctionName (NameNested (NestedTemplateName TemplatePrefix
pr NonEmpty TemplateArg
_ [CVQualifier]
_ Maybe RefQualifier
_)) ->
         case TemplatePrefix
pr of
           NestedTemplate Prefix
_pfx NonEmpty UnqualifiedName
uqns | CtorDtorName {} <- forall a. NonEmpty a -> a
NEL.last NonEmpty UnqualifiedName
uqns -> Maybe (NextArg Encoding)
noRetType
           TemplatePrefix
_ -> Maybe (NextArg Encoding)
withRetType
       FunctionName
_ ->
#ifdef MIN_VERSION_panic
            -- traceM ("bft what??! " <> show (i ^. nVal)) >>
#endif
            forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (FunctionName -> Maybe Type_ -> NonEmpty Type_ -> Encoding
constr (NextArg FunctionName
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) forall a. Maybe a
Nothing) NextArg (NonEmpty Type_)
tys

-- | Called to return one or more types.  This is essentially the same as `some'
-- type_`, but also handles the case where there might be multiple types returned
-- at once by a Template Argument Pack.

types_ :: AnyNext (NEL.NonEmpty Type_)
types_ :: AnyNext (NonEmpty Type_)
types_ = forall a b. AnyNext a -> Next b (NonEmpty a)
some' AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap forall a. Semigroup a => NonEmpty a -> a
sconcat

-- | Called to parse a *single* type.  If multiple types are obtained (e.g. an
-- Template Argument Pack), return Nothing indicating a parse failure.

type_ :: AnyNext Type_
type_ :: AnyNext Type_
type_ = AnyNext (NonEmpty Type_)
type_parser
        forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> \NextArg (NonEmpty Type_)
i -> case NextArg (NonEmpty Type_)
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal of
                    Type_
ty :| [] -> forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg (NonEmpty Type_)
i Type_
ty
                    NonEmpty Type_
_ -> forall a b. a -> String -> [String] -> Maybe b
cannot Demangler
Demangler String
"type_"
                         [ String
"Can only handle a type_parser return of one type"
                         , String
"to respond to a location expecting only one."
                         , String
"bad1: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (NextArg (NonEmpty Type_)
iforall s a. s -> Getting a s a -> a
^.forall a b. Lens (NextArg a) (NextArg b) a b
nVal)
                         , String
" rem: " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (NextArg (NonEmpty Type_)
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Text
nInp)
                         ]


-- | Returns one or more types.  Normally this should only parse a single type
-- entry, but in the case of Template argument packs, there could actually be
-- multiple types returned.  This should be used as an internal function; callers
-- should use one of the types_ or type_ wrappers to indicate if they can handle
-- multiple types or not.

type_parser :: AnyNext (NEL.NonEmpty Type_)
type_parser :: AnyNext (NonEmpty Type_)
type_parser =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [
          -- Matches that are not type substitution candidates
          forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap ((forall a. a -> [a] -> NonEmpty a
:|[]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. BaseType -> Type_
BaseType) forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< AnyNext BaseType
builtin_type

          -- Single element matches
        , forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext Type_
qualified_type
                , AnyNext Type_
function_type
                , AnyNext Type_
class_enum_type
                , AnyNext Type_
array_type
                  -- , pointer_to_member_type
                , AnyNext TemplateArg
template_template_param forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext (NonEmpty TemplateArg)
template_args
                  forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry TemplateArg -> NonEmpty TemplateArg -> Type_
Template)
                  -- , decltype
                -- This one is tricky: it's recursive, but then binds the
                -- (possibly) multiple returned recursion types into a single
                -- type.
                , forall a. Text -> Next a a
match Text
"Dp" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap NonEmpty Type_ -> Type_
Cpp11PackExpansion
                ]
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next Type_ Type_
canSubstType
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a. a -> [a] -> NonEmpty a
:|[])

          -- Possibly multiple element matches (either direct or via recursion)
        , forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"P" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type_ -> Type_
Pointer)
                , forall a. Text -> Next a a
match Text
"R" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type_ -> Type_
LValRef)
                , forall a. Text -> Next a a
match Text
"O" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type_ -> Type_
RValRef)
                , forall a. Text -> Next a a
match Text
"C" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type_ -> Type_
ComplexPair)
                , forall a. Text -> Next a a
match Text
"G" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
type_parser forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Type_ -> Type_
Imaginary)
                , AnyNext TemplateArg
template_param
                  forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (\NextArg TemplateArg
i ->
                         let retType :: Type_ -> Maybe (NextArg (NonEmpty Type_))
retType Type_
t = forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg TemplateArg
i (Type_
t forall a. a -> [a] -> NonEmpty a
:| [])
                         in case NextArg TemplateArg
i forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal of
                              TArgType Type_
t -> Type_ -> Maybe (NextArg (NonEmpty Type_))
retType Type_
t
                              TArgPack [] -> Type_ -> Maybe (NextArg (NonEmpty Type_))
retType forall a b. (a -> b) -> a -> b
$ BaseType -> Type_
BaseType BaseType
Ellipsis
                              TArgPack [TemplateArg]
tas ->
                                let each :: TemplateArg -> Type_
each = \case
                                      TArgType Type_
t -> Type_
t
                                      TemplateArg
_ -> forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ forall a b. a -> String -> [String] -> Maybe b
cannot Demangler
Demangler
                                           String
"type_parser.template_param"
                                           [ String
"nested TArgPack members:"
                                           , forall a. Show a => a -> String
show [TemplateArg]
tas
                                           ]
                                in forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg TemplateArg
i forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. [a] -> Maybe (NonEmpty a)
NEL.nonEmpty (TemplateArg -> Type_
each forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [TemplateArg]
tas)
                              TemplateArg
o -> forall a b. a -> String -> [String] -> Maybe b
cannot Demangler
Demangler String
"type_parser.template_param"
                                   [ String
"bad template param ref in type:"
                                   , forall (saytag :: Symbol) a. Sayable saytag a => a -> String
sez @"debug" (forall a. a -> Context -> WithContext a
WC TemplateArg
o (NextArg TemplateArg
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Context
nContext))
                                   , String
"raw: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show TemplateArg
o
                                   ]
                      )
                ]
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next (NonEmpty Type_) (NonEmpty Type_)
canSubstTypes

          -- Substitutions, which are not recursively added as a substitution
          -- candidate
        , AnyNext Substitution'
substitution
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next Substitution Type_ -> Next Substitution' Type_
substituteType Next Substitution Type_
stdSubstToType
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a. a -> [a] -> NonEmpty a
:|[])
        ]


builtin_type :: AnyNext BaseType
builtin_type :: AnyNext BaseType
builtin_type =
  let parse :: (b, (Text, b, c)) -> NextArg a -> Maybe (NextArg b)
parse (b
t,(Text
m,b
_,c
_)) = forall a. Text -> Next a a
match Text
m forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' b
t
  in forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum'
     ((forall {b} {b} {c} {a}.
(b, (Text, b, c)) -> NextArg a -> Maybe (NextArg b)
parse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(BaseType, (Text, Text, Text))]
builtinTypeTable)
       forall a. Semigroup a => a -> a -> a
<> [ forall a. Text -> Next a a
match Text
"DF" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Natural -> BaseType
FloatN  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => Int -> a
toEnum) forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_"
          , forall a. Text -> Next a a
match Text
"DF" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Natural -> BaseType
FloatNx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => Int -> a
toEnum) forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"x"
          , forall a. Text -> Next a a
match Text
"DB" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Natural -> BaseType
SBitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => Int -> a
toEnum) forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_" -- TODO: or expression
          , forall a. Text -> Next a a
match Text
"DU" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Natural -> BaseType
UBitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => Int -> a
toEnum) forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_" -- TODO: or expression
          , forall a. Text -> Next a a
match Text
"u" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext SourceName
source_name forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a b. Next a b -> Next a (a, Maybe b)
optional' AnyNext (NonEmpty TemplateArg)
template_args
            forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry SourceName -> Maybe (NonEmpty TemplateArg) -> BaseType
VendorExtendedType)
          ]
     )

qualified_type :: AnyNext Type_
qualified_type :: AnyNext Type_
qualified_type NextArg a
i = do NextArg [()]
eQ <- forall a. Next () a -> Next () [a]
many' Next () ()
extended_qualifier forall a b. (a -> b) -> a -> b
$ forall a.
NextArg a
-> (Text,
    ((),
     (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
rdiscard NextArg a
i
                      NextArg [CVQualifier]
cQ <- AnyNext [CVQualifier]
cv_qualifiers NextArg [()]
eQ
                      -- Require some amount of production before recursion
                      Bool -> Maybe ()
require forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall a b. (a -> b) -> a -> b
$ NextArg [CVQualifier]
cQ forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                                          , forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall a b. (a -> b) -> a -> b
$ NextArg [()]
eQ forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                                          ]
                      NextArg Type_
tY <- AnyNext Type_
type_ NextArg [CVQualifier]
cQ
                      forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg Type_
tY forall a b. (a -> b) -> a -> b
$ [()] -> [CVQualifier] -> Type_ -> Type_
QualifiedType (NextArg [()]
eQ forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg [CVQualifier]
cQ forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg Type_
tY forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)

extended_qualifier :: Next () ExtendedQualifier
extended_qualifier :: Next () ()
extended_qualifier = forall a b. String -> Next a b
tbd String
"extended_qualifier"

function_type :: AnyNext Type_
-- function_type = tbd "function_type"
function_type :: AnyNext Type_
function_type NextArg a
i = do NextArg
  (([CVQualifier], Maybe ExceptionSpec),
   Maybe ([CVQualifier], Maybe ExceptionSpec))
f0 <- AnyNext [CVQualifier]
cv_qualifiers NextArg a
i
                           forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a b. Next a b -> Next a (a, Maybe b)
optional' AnyNext ExceptionSpec
exception_spec
                           forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a b. Next a b -> Next a (a, Maybe b)
optional' (forall a. Text -> Next a a
match Text
"Dx")
                     let (([CVQualifier]
cvq, Maybe ExceptionSpec
mb'exc), Maybe ([CVQualifier], Maybe ExceptionSpec)
mb'dx) = NextArg
  (([CVQualifier], Maybe ExceptionSpec),
   Maybe ([CVQualifier], Maybe ExceptionSpec))
f0 forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                     NextArg
  ((([CVQualifier], Maybe ExceptionSpec),
    Maybe ([CVQualifier], Maybe ExceptionSpec)),
   Maybe
     (([CVQualifier], Maybe ExceptionSpec),
      Maybe ([CVQualifier], Maybe ExceptionSpec)))
f1 <- forall a. Text -> Next a a
match Text
"F" NextArg
  (([CVQualifier], Maybe ExceptionSpec),
   Maybe ([CVQualifier], Maybe ExceptionSpec))
f0 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a b. Next a b -> Next a (a, Maybe b)
optional' (forall a. Text -> Next a a
match Text
"Y")
                     let isExternC :: Bool
isExternC = forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ NextArg
  ((([CVQualifier], Maybe ExceptionSpec),
    Maybe ([CVQualifier], Maybe ExceptionSpec)),
   Maybe
     (([CVQualifier], Maybe ExceptionSpec),
      Maybe ([CVQualifier], Maybe ExceptionSpec)))
f1 forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                     NextArg (NonEmpty Type_)
tys <- AnyNext (NonEmpty Type_)
types_ NextArg
  ((([CVQualifier], Maybe ExceptionSpec),
    Maybe ([CVQualifier], Maybe ExceptionSpec)),
   Maybe
     (([CVQualifier], Maybe ExceptionSpec),
      Maybe ([CVQualifier], Maybe ExceptionSpec)))
f1
                     let (Type_
rty, Maybe (NonEmpty Type_)
tyrem) = forall a. NonEmpty a -> (a, Maybe (NonEmpty a))
NEL.uncons forall a b. (a -> b) -> a -> b
$ NextArg (NonEmpty Type_)
tys forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal
                     NonEmpty Type_
argtys <- Maybe (NonEmpty Type_)
tyrem
                     NextArg (NonEmpty Type_, Maybe RefQualifier)
f2 <- forall a b. Next a b -> Next a (a, Maybe b)
optional' AnyNext RefQualifier
ref_qualifier NextArg (NonEmpty Type_)
tys
                           forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Text -> Next a a
match Text
"E"
                     let trnsct :: Transaction
trnsct = if forall a. Maybe a -> Bool
isJust Maybe ([CVQualifier], Maybe ExceptionSpec)
mb'dx
                                  then Transaction
TransactionSafe
                                  else Transaction
TransactionUnsafe
                     forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg (NonEmpty Type_, Maybe RefQualifier)
f2 ([CVQualifier]
-> Maybe ExceptionSpec
-> Transaction
-> Bool
-> Type_
-> NonEmpty Type_
-> Maybe RefQualifier
-> Type_
Function [CVQualifier]
cvq Maybe ExceptionSpec
mb'exc Transaction
trnsct Bool
isExternC
                             Type_
rty NonEmpty Type_
argtys (forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ NextArg (NonEmpty Type_, Maybe RefQualifier)
f2 forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal))

exception_spec :: AnyNext ExceptionSpec
exception_spec :: AnyNext ExceptionSpec
exception_spec = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"Do" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' ExceptionSpec
NonThrowing
                       , forall a. Text -> Next a a
match Text
"DO" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Expression
expression forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E"
                         forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Expression -> ExceptionSpec
ComputedThrow
                       , forall a. Text -> Next a a
match Text
"Dw" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext (NonEmpty Type_)
types_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E"
                         forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap NonEmpty Type_ -> ExceptionSpec
Throwing
                       ]

class_enum_type :: AnyNext Type_
class_enum_type :: AnyNext Type_
class_enum_type = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Name -> Type_
ClassUnionStructEnum forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< AnyNext Name
name
                        , forall a. Text -> Next a a
match Text
"Ts" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Name -> Type_
ClassStruct forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< AnyNext Name
name
                        , forall a. Text -> Next a a
match Text
"Tu" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Name -> Type_
Union forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< AnyNext Name
name
                        , forall a. Text -> Next a a
match Text
"Te" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Name -> Type_
Enum forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< AnyNext Name
name
                        ]

array_type :: AnyNext Type_
array_type :: AnyNext Type_
array_type = forall a. Text -> Next a a
match Text
"A"
             forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"_" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (ArrayBound -> Type_ -> Type_
ArrayType ArrayBound
NoBounds)
                       , AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_" forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext Type_
type_
                         forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ArrayBound -> Type_ -> Type_
ArrayType forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ArrayBound
NumBound))
                       , AnyNext Expression
expression forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_" forall a b c. Next a b -> Next b c -> Next a (b, c)
>&=> AnyNext Type_
type_
                         forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ArrayBound -> Type_ -> Type_
ArrayType forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expression -> ArrayBound
ExprBound))
                       ]

function_encoding :: AnyNext FunctionScope
function_encoding :: AnyNext Int
function_encoding = forall a b. String -> Next a b
tbd String
"function_encoding"

entity_name :: Next a FunctionEntity
entity_name :: AnyNext Int
entity_name = forall a b. String -> Next a b
tbd String
"entity_name"

discriminator :: Next a Int
discriminator :: AnyNext Int
discriminator = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"_" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
single_digit_num
                      , forall a. Text -> Next a a
match Text
"__" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_"
                      ]

template_prefix_and_args :: AnyNext (TemplatePrefix, Maybe TemplateArgs)
template_prefix_and_args :: AnyNext (TemplatePrefix, Maybe (NonEmpty TemplateArg))
template_prefix_and_args =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ \NextArg a
i -> do NextArg Prefix
p <- AnyNext Prefix
prefix NextArg a
i
                           (Prefix
tpr, Either UnqualifiedName (NonEmpty TemplateArg)
entas) <- Prefix
-> Maybe (Prefix, Either UnqualifiedName (NonEmpty TemplateArg))
prefixInitLast (NextArg Prefix
p forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)
                           (Prefix
rmnpval, Either UnqualifiedName (NonEmpty TemplateArg)
ent) <- Prefix
-> Maybe (Prefix, Either UnqualifiedName (NonEmpty TemplateArg))
prefixInitLast Prefix
tpr
                           case Either UnqualifiedName (NonEmpty TemplateArg)
ent of
                             Right NonEmpty TemplateArg
_ ->
                               forall a b. a -> String -> [String] -> Maybe b
cannot Demangler
Demangler String
"template_prefix_and_args"
                               [String
"Penultimate prefix must be an UnqualifiedName"]
                             Left UnqualifiedName
un ->
                               -- ultimate prefix entry must be template_args
                               case Either UnqualifiedName (NonEmpty TemplateArg)
entas of
                                 Left UnqualifiedName
_ -> forall a. Maybe a
Nothing
                                 Right NonEmpty TemplateArg
tas ->
                                   let constr :: NonEmpty UnqualifiedName -> TemplatePrefix
constr = case Prefix
rmnpval of
                                                  Prefix
EmptyPrefix -> NonEmpty UnqualifiedName -> TemplatePrefix
GlobalTemplate
                                                  Prefix
p' -> Prefix -> NonEmpty UnqualifiedName -> TemplatePrefix
NestedTemplate Prefix
p'
                                   in forall (f :: * -> *) a b.
Applicative f =>
NextArg a -> b -> f (NextArg b)
ret NextArg Prefix
p (NonEmpty UnqualifiedName -> TemplatePrefix
constr (UnqualifiedName
un forall a. a -> [a] -> NonEmpty a
:| []), forall a. a -> Maybe a
Just NonEmpty TemplateArg
tas)
                   , AnyNext TemplateArg
template_param forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap ((, forall a. Maybe a
Nothing) forall b c a. (b -> c) -> (a -> b) -> a -> c
. TemplateArg -> TemplatePrefix
TemplateTemplateParam)
                   ]
           , AnyNext Substitution'
substitution forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next Substitution TemplatePrefix
-> Next Substitution' TemplatePrefix
substituteTemplatePrefix
             (\NextArg Substitution
s -> forall a b. a -> String -> [String] -> Maybe b
cannot Demangler
Demangler String
"template_prefix_and_args"
                    [ String
"Not a template prefix substitute:"
                    , forall a. Show a => a -> String
show (NextArg Substitution
s forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)
                    ]
             )
             forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (, forall a. Maybe a
Nothing)
           ]


template_template_param :: AnyNext TemplateParam
template_template_param :: AnyNext TemplateArg
template_template_param NextArg a
i = (AnyNext TemplateArg
template_param NextArg a
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Next TemplateArg TemplateArg
canSubstTemplateParam)
                            forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (AnyNext Substitution'
substitution NextArg a
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a b. String -> Next a b
tbd String
"ttp subs")

-- | Process a set of template args.
--
-- Note that only the *outermost* and *final* set of template args should be
-- candidates for substitution.
--
--   foo<bar<int> >              --->   [ bar<int> ]
--   foo<int>::bar<float, char>  --->  [ float, char ]
--
-- The nTmplSubsLatch and lTmplSubsLock form a two-phase state management: both
-- start as false and lock is one cycle after latch.  When locked, no new
-- template substitutions can be made.  Thus, recursion is handled by the first
-- (outermost) set of template parameters setting latch, which means that any
-- subsequent of (recursive) template parameters will set lock and therefore will
-- not update any template parameter substitution candidates.
--
-- The *final* set is handled by clearing the set of template parameter
-- substitution candidates any time this is entered and latch isn't set (latch is
-- cleared when each outer-level template parameter parsing is completed or has
-- not yet started).
--
-- One additional constraint is that template parameters are only substitution
-- candidates if encountered in the function "name" portion, but once function
-- arguments are processed, no more template argument candidates are added.  This
-- is accomplished by the argument processing setting the lock, and not doing any
-- clearing here when lock is set.

template_args :: AnyNext TemplateArgs
template_args :: AnyNext (NonEmpty TemplateArg)
template_args = forall a. Text -> Next a a
match Text
"I"
                forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (\NextArg a
i -> do let latched :: Bool
latched = NextArg a
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Bool
nTmplSubsLatch
                                  locked :: Bool
locked = NextArg a
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Bool
nTmplSubsLock
                                  lock :: Bool
lock = Bool
latched
                                  enter :: NextArg a -> NextArg a
enter = (forall a. Lens' (NextArg a) Bool
nTmplSubsLatch forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
True)
                                          forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Lens' (NextArg a) Bool
nTmplSubsLock forall s t. ASetter s t Bool Bool -> Bool -> s -> t
||~ Bool
lock)
                                  i' :: NextArg a
i' = if Bool
latched Bool -> Bool -> Bool
|| Bool
locked
                                          -- reads: if outermost and not reading
                                          -- function arguments
                                       then NextArg a
i forall a b. a -> (a -> b) -> b
& NextArg a -> NextArg a
enter
                                       else NextArg a
i forall a b. a -> (a -> b) -> b
& NextArg a -> NextArg a
enter forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) (Seq TemplateArg)
nTmplSubs forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a. Monoid a => a
mempty
                              NextArg (NonEmpty TemplateArg)
r <- forall a b. AnyNext a -> Next b (NonEmpty a)
some' AnyNext TemplateArg
template_arg NextArg a
i'
                              forall (f :: * -> *) a. Applicative f => a -> f a
pure
                                forall a b. (a -> b) -> a -> b
$ NextArg (NonEmpty TemplateArg)
r
                                forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Bool
nTmplSubsLatch forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
latched
                                forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Bool
nTmplSubsLock forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
locked
                    )
                forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E"

template_arg :: AnyNext TemplateArg
template_arg :: AnyNext TemplateArg
template_arg =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> TemplateArg
TArgType forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next TemplateArg TemplateArg
canSubstTemplateArg
        , forall a. Text -> Next a a
match Text
"X" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Expression
expression forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Expression -> TemplateArg
TArgExpr
        , AnyNext ExprPrimary
expression_primary forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap ExprPrimary -> TemplateArg
TArgSimpleExpr forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next TemplateArg TemplateArg
canSubstTemplateArg
        , forall a. Text -> Next a a
match Text
"J"
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (\NextArg a
i -> do let locked :: Bool
locked = NextArg a
i forall s a. s -> Getting a s a -> a
^. forall a. Lens' (NextArg a) Bool
nTmplSubsLock
                        NextArg [TemplateArg]
r <- forall a. Next () a -> Next () [a]
many' AnyNext TemplateArg
template_arg forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
NextArg a
-> (Text,
    ((),
     (Context, (Seq SubsCandidate, (Seq TemplateArg, Bool, Bool)))))
rdiscard forall a b. (a -> b) -> a -> b
$ NextArg a
i forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Bool
nTmplSubsLock forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
True
                        NextArg [TemplateArg]
l <- forall a. Text -> Next a a
match Text
"E" NextArg [TemplateArg]
r
                        forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap [TemplateArg] -> TemplateArg
TArgPack forall a b. (a -> b) -> a -> b
$ NextArg [TemplateArg]
l forall a b. a -> (a -> b) -> b
& forall a. Lens' (NextArg a) Bool
nTmplSubsLock forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
locked
              )
          forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next TemplateArg TemplateArg
canSubstTemplateArg
        ]

template_param :: AnyNext TemplateParam
template_param :: AnyNext TemplateArg
template_param =
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"T_" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) b a.
Applicative f =>
b -> NextArg a -> f (NextArg b)
ret' forall a. Maybe a
Nothing
        , forall a. Text -> Next a a
match Text
"T" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Int
digits_num forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap forall a. a -> Maybe a
Just forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"_"
        ]
  forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Next (Maybe Int) TemplateArg
substituteTemplateParam

expression :: AnyNext Expression
expression :: AnyNext Expression
expression = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ forall a. Text -> Next a a
match Text
"sp" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Expression
expression forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Expression -> Expression
ExprPack
                   , AnyNext TemplateArg
template_param forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap TemplateArg -> Expression
ExprTemplateParam
                   , AnyNext ExprPrimary
expression_primary forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap ExprPrimary -> Expression
ExprPrim
                   , forall a b. String -> Next a b
tbd String
"expression"
                   ]

expression_primary :: AnyNext ExprPrimary
expression_primary :: AnyNext ExprPrimary
expression_primary =
  let toFloat :: a -> a -> a
toFloat a
w a
f = forall a. Read a => String -> a
read (forall a. Show a => a -> String
show a
w forall a. Semigroup a => a -> a -> a
<> String
"." forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show a
f)
      floatLit :: NextArg Type_ -> NextArg a -> a -> ExprPrimary
floatLit NextArg Type_
ty NextArg a
w a
p = Type_ -> Float -> ExprPrimary
FloatLit (NextArg Type_
ty forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) forall a b. (a -> b) -> a -> b
$ forall {a} {a} {a}. (Read a, Show a, Show a) => a -> a -> a
toFloat (NextArg a
w forall s a. s -> Getting a s a -> a
^.forall a b. Lens (NextArg a) (NextArg b) a b
nVal) a
p
      complexLit :: NextArg Type_
-> NextArg a -> NextArg a -> NextArg a -> a -> ExprPrimary
complexLit NextArg Type_
ty NextArg a
w NextArg a
p NextArg a
iw a
ip =
        Type_ -> Float -> Float -> ExprPrimary
ComplexFloatLit (NextArg Type_
ty forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (forall {a} {a} {a}. (Read a, Show a, Show a) => a -> a -> a
toFloat (NextArg a
w forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) (NextArg a
p forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)) (forall {a} {a} {a}. (Read a, Show a, Show a) => a -> a -> a
toFloat (NextArg a
iw forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal) a
ip)
      withType :: NextArg Type_ -> Maybe (NextArg ExprPrimary)
withType NextArg Type_
t = forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [ do NextArg Int
d <- AnyNext Int
digits_num NextArg Type_
t
                             forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [ do NextArg Int
r <- forall a. Text -> Next a a
match Text
"." NextArg Int
d forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AnyNext Int
digits_num
                                       forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [ do NextArg Int
i <- forall a. Text -> Next a a
match Text
"_" NextArg Int
r
                                                      forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AnyNext Int
digits_num
                                                      forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Text -> Next a a
match Text
"."
                                                 NextArg Int
ir <- AnyNext Int
digits_num NextArg Int
i
                                                 forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall {a} {a} {a} {a}.
(Show a, Show a, Show a, Show a) =>
NextArg Type_
-> NextArg a -> NextArg a -> NextArg a -> a -> ExprPrimary
complexLit NextArg Type_
t NextArg Int
d NextArg Int
r NextArg Int
i) NextArg Int
ir
                                            , forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (forall {a} {a}.
(Show a, Show a) =>
NextArg Type_ -> NextArg a -> a -> ExprPrimary
floatLit NextArg Type_
t NextArg Int
d) NextArg Int
r
                                            ]
                                  , forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap (Type_ -> Int -> ExprPrimary
IntLit (NextArg Type_
t forall s a. s -> Getting a s a -> a
^. forall a b. Lens (NextArg a) (NextArg b) a b
nVal)) NextArg Int
d
                                  ]
                        , forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Type_ -> ExprPrimary
DirectLit NextArg Type_
t
                        ]
  in
    forall a. Text -> Next a a
match Text
"L"
    forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Functor t, Alternative f) =>
t (a -> f b) -> a -> f b
asum' [ AnyNext Type_
type_ forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> NextArg Type_ -> Maybe (NextArg ExprPrimary)
withType forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E"
              , forall a. Text -> Next a a
match Text
"_Z" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> AnyNext Encoding
encoding forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall a. Text -> Next a a
match Text
"E" forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a b.
Applicative f =>
(a -> b) -> NextArg a -> f (NextArg b)
rmap Encoding -> ExprPrimary
ExternalNameLit
              ]