RFC: A mechanism for importing a declaration only when it exists

Message: 4
Date: Fri, 26 Jun 2020 00:05:17 -0400
From: Louis Dionne via cfe-dev <cfe-dev@lists.llvm.org>
To: Clang Dev <cfe-dev@lists.llvm.org>
Subject: [cfe-dev] RFC: A mechanism for importing a declaration only
when it exists
Message-ID: <B287F98A-A416-489E-B122-B78468303C3C@apple.com>
Content-Type: text/plain; charset=us-ascii


The C++ Standard Library has multiple headers of the form , which are
basically importing declarations from the corresponding <foobar.h> header into
namespace std. For example, basically imports all the declarations from
<stdio.h> into namespace std:

namespace std {
using ::FILE;
using ::fpos_t;
using ::size_t;


When porting libc++ to new platforms, especially more embedded-ish platforms,
it is very common that some declarations are not provided by the underlying
C Standard Library. This leads to having to detect what platform we’re on,
and then to conditionally exclude some declarations:

namespace std {
using ::FILE;
using ::fpos_t;
using ::size_t;

Unfortunately, different platforms often offer slightly different subsets, so
these #ifs quickly become difficult to maintain. Trying to find common themes
for excluding declarations (e.g. #if !defined(_LIBCPP_HAS_NO_FILE)) is vain,
because the subset provided by a platform is often arbitrary. For example, I’ve
seen platforms where malloc() and free() were not provided, however operator new
was – so trying to carve out something like _LIBCPP_HAS_NO_ALLOCATION wouldn’t
really make sense.

Given the difficulty of manually excluding such using declarations, I came to
the conclusion that what we wanted was often something like (pseudocode):

namespace std {
#if __has_declaration(::FILE)
using ::FILE;

#if __has_declaration(::fpos_t)
using ::fpos_t;

#if __has_declaration(::size_t)
using ::size_t;


Basically, we want to import each declaration into namespace std only if the
global namespace has such a declaration.

Now, I understand this raises several questions. Of course, this couldn’t be
done at the preprocessor level because I don’t think we can know whether a
declaration exists at that time. However, I was curious to ask on this list
whether someone could think of a reasonable way to solve this problem.

Having some mechanism for doing this would be a huge life improvement for libc++.


I’ll take a shot. About a year ago I developed what I call a “metaparse” statement node for clang, intended to work with a reflection implementation I developed alongside it, which in a sense brings preprocessor expansion ability into constexpr evaluation.

Assume for now __has_declaration is already defined:

consteval { //a "metaprogram"

if (__has_declaration(::FILE))
__queue_metaparse("using ::FILE;");

if (__has_declaration(::fpos_t))
__queue_metaparse("using ::fpos_t;");

if (__has_declaration(::size_t))
__queue_metaparse("using ::size_t;");

__queue_metaparse("static const int TestVar = 3;");

} //...queued metaparses performed here, placing parsed decls into outer scope...


If you wanted to define __has_declaration via what I call my “mirror-image reflection” implem, rather than via a new built-in: you would first probably want to define a public clang::DeclContext method “bool hasDeclNamed(StringRef name)” to handle the lookup, then after building clang-wreflection that method would be available in all reflected DeclContexts. Then:

#define __has_declaration(NAME) reflexpr(::/*reflects the translation unit*/)->hasDeclNamed(#NAME)

The following is old code I have not updated in awhile, and the readme/documentation is a mess, but you’ll get the gist.

The reflection stuff did not catch on here because the clang folks did not want the clang AST interface to be exposed via reflection, as it is not stable. But I was very surprised at least the metaparsing portion did not catch on — it’s a great general purpose tool that would work with any reflection implementation, or in other metaprogramming situations such as yours, and is dirt-simple to understand and use.

FWIW Microsoft seemed to get the idea, and several months after my release added what they call “source generators” https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/ to C# which seems to work in this way. (See their sourceBuilder.append(…) stuff. No attribution though :/).

Bottom line, to do these sorts of things in general we need reflection + its inverse (“reification” as some call it), and metaparsing or source generation or whatever you want to call seems to be the most straightforward and general solution for that latter task.

Hope that helps the brainstorming, good luck,