This fixes problem described as bug:31405
https://bugs.llvm.org//show_bug.cgi?id=31405
Itanium ABI: debug assertion in template mangling with declype return
The problem is that FunctionTypeDepthState is not properly managed in templated decltype() construct mangling resulting in assertion inside mangler
mangler: /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:3976: void (anonymous namespace)::CXXNameMangler::mangleFunctionParam(const clang::ParmVarDecl *): Assertion `parmDepth < FunctionTypeDepth.getDepth()' failed.
Aborted (core dumped)
for code as simple as template <typename T> auto foo(T x) -> const decltype(x);
The fix is to properly save/restore FunctionTypeDepthState before/after mangling of type in decltype. This exactly same way FunctionTypeDepthState treated in other similar places in a mangler.
While in normal operation mangling of non-instantiated templates is not needed, some tools like source code indexers and others may need this. Also in most cases mangling of template functions does work, so described assertion looks like a flaw rather than actual guard.
See issue description above for more details including stack trace of the issue, reproducer example and simple mangling matcher exhibiting the issue.