Linkage.h revision 263508
1255670Sdes//===--- Linkage.h - Linkage enumeration and utilities ----------*- C++ -*-===//
298937Sdes//
398937Sdes//                     The LLVM Compiler Infrastructure
498937Sdes//
598937Sdes// This file is distributed under the University of Illinois Open Source
698937Sdes// License. See LICENSE.TXT for details.
798937Sdes//
898937Sdes//===----------------------------------------------------------------------===//
998937Sdes///
1098937Sdes/// \file
1198937Sdes/// \brief Defines the Linkage enumeration and various utility functions.
1298937Sdes///
1398937Sdes//===----------------------------------------------------------------------===//
1498937Sdes#ifndef LLVM_CLANG_BASIC_LINKAGE_H
1598937Sdes#define LLVM_CLANG_BASIC_LINKAGE_H
1698937Sdes
1798937Sdesnamespace clang {
1898937Sdes
19113908Sdes/// \brief Describes the different kinds of linkage
20113908Sdes/// (C++ [basic.link], C99 6.2.2) that an entity may have.
21124208Sdesenum Linkage {
22124208Sdes  /// \brief No linkage, which means that the entity is unique and
23128456Sdes  /// can only be referred to from within its scope.
24128456Sdes  NoLinkage = 0,
25128456Sdes
26113908Sdes  /// \brief Internal linkage, which indicates that the entity can
2798937Sdes  /// be referred to from within the translation unit (but not other
28113908Sdes  /// translation units).
2998937Sdes  InternalLinkage,
3098937Sdes
3198937Sdes  /// \brief External linkage within a unique namespace.
3298937Sdes  ///
3398937Sdes  /// From the language perspective, these entities have external
34113908Sdes  /// linkage. However, since they reside in an anonymous namespace,
35126274Sdes  /// their names are unique to this translation unit, which is
3698937Sdes  /// equivalent to having internal linkage from the code-generation
3798937Sdes  /// point of view.
3898937Sdes  UniqueExternalLinkage,
3998937Sdes
4098937Sdes  /// \brief No linkage according to the standard, but is visible from other
4198937Sdes  /// translation units because of types defined in a inline function.
4298937Sdes  VisibleNoLinkage,
4398937Sdes
4498937Sdes  /// \brief External linkage, which indicates that the entity can
4598937Sdes  /// be referred to from other translation units.
4698937Sdes  ExternalLinkage
4798937Sdes};
4898937Sdes
4998937Sdes/// \brief Describes the different kinds of language linkage
5098937Sdes/// (C++ [dcl.link]) that an entity may have.
5198937Sdesenum LanguageLinkage {
5298937Sdes  CLanguageLinkage,
5398937Sdes  CXXLanguageLinkage,
5498937Sdes  NoLanguageLinkage
5598937Sdes};
5698937Sdes
5798937Sdes/// \brief A more specific kind of linkage than enum Linkage.
5898937Sdes///
5998937Sdes/// This is relevant to CodeGen and AST file reading.
6098937Sdesenum GVALinkage {
6198937Sdes  GVA_Internal,
6298937Sdes  GVA_C99Inline,
6398937Sdes  GVA_CXXInline,
6498937Sdes  GVA_StrongExternal,
6598937Sdes  GVA_TemplateInstantiation,
6698937Sdes  GVA_ExplicitTemplateInstantiation
6798937Sdes};
6898937Sdes
6998937Sdesinline bool isExternallyVisible(Linkage L) {
7098937Sdes  return L == ExternalLinkage || L == VisibleNoLinkage;
7198937Sdes}
7298937Sdes
73255670Sdesinline Linkage getFormalLinkage(Linkage L) {
7498937Sdes  if (L == UniqueExternalLinkage)
7598937Sdes    return ExternalLinkage;
7698937Sdes  if (L == VisibleNoLinkage)
7798937Sdes    return NoLinkage;
7898937Sdes  return L;
7998937Sdes}
8098937Sdes
8198937Sdesinline bool isExternalFormalLinkage(Linkage L) {
8298937Sdes  return getFormalLinkage(L) == ExternalLinkage;
8398937Sdes}
8498937Sdes
8598937Sdes/// \brief Compute the minimum linkage given two linkages.
8698937Sdes///
8798937Sdes/// The linkage can be interpreted as a pair formed by the formal linkage and
8898937Sdes/// a boolean for external visibility. This is just what getFormalLinkage and
8998937Sdes/// isExternallyVisible return. We want the minimum of both components. The
9098937Sdes/// Linkage enum is defined in an order that makes this simple, we just need
9198937Sdes/// special cases for when VisibleNoLinkage would lose the visible bit and
9298937Sdes/// become NoLinkage.
9398937Sdesinline Linkage minLinkage(Linkage L1, Linkage L2) {
9498937Sdes  if (L2 == VisibleNoLinkage)
9598937Sdes    std::swap(L1, L2);
9698937Sdes  if (L1 == VisibleNoLinkage) {
9798937Sdes    if (L2 == InternalLinkage)
9898937Sdes      return NoLinkage;
9998937Sdes    if (L2 == UniqueExternalLinkage)
10098937Sdes      return NoLinkage;
10198937Sdes  }
10298937Sdes  return L1 < L2 ? L1 : L2;
10398937Sdes}
10498937Sdes
10598937Sdes} // end namespace clang
10698937Sdes
10798937Sdes#endif // LLVM_CLANG_BASIC_LINKAGE_H
10898937Sdes