clang API Documentation

Linkage.h
Go to the documentation of this file.
00001 //===--- Linkage.h - Linkage enumeration and utilities ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Defines the Linkage enumeration and various utility functions.
00012 ///
00013 //===----------------------------------------------------------------------===//
00014 #ifndef LLVM_CLANG_BASIC_LINKAGE_H
00015 #define LLVM_CLANG_BASIC_LINKAGE_H
00016 
00017 namespace clang {
00018 
00019 /// \brief Describes the different kinds of linkage 
00020 /// (C++ [basic.link], C99 6.2.2) that an entity may have.
00021 enum Linkage : unsigned char {
00022   /// \brief No linkage, which means that the entity is unique and
00023   /// can only be referred to from within its scope.
00024   NoLinkage = 0,
00025 
00026   /// \brief Internal linkage, which indicates that the entity can
00027   /// be referred to from within the translation unit (but not other
00028   /// translation units).
00029   InternalLinkage,
00030 
00031   /// \brief External linkage within a unique namespace. 
00032   ///
00033   /// From the language perspective, these entities have external
00034   /// linkage. However, since they reside in an anonymous namespace,
00035   /// their names are unique to this translation unit, which is
00036   /// equivalent to having internal linkage from the code-generation
00037   /// point of view.
00038   UniqueExternalLinkage,
00039 
00040   /// \brief No linkage according to the standard, but is visible from other
00041   /// translation units because of types defined in a inline function.
00042   VisibleNoLinkage,
00043 
00044   /// \brief External linkage, which indicates that the entity can
00045   /// be referred to from other translation units.
00046   ExternalLinkage
00047 };
00048 
00049 /// \brief Describes the different kinds of language linkage
00050 /// (C++ [dcl.link]) that an entity may have.
00051 enum LanguageLinkage {
00052   CLanguageLinkage,
00053   CXXLanguageLinkage,
00054   NoLanguageLinkage
00055 };
00056 
00057 /// \brief A more specific kind of linkage than enum Linkage.
00058 ///
00059 /// This is relevant to CodeGen and AST file reading.
00060 enum GVALinkage {
00061   GVA_Internal,
00062   GVA_AvailableExternally,
00063   GVA_DiscardableODR,
00064   GVA_StrongExternal,
00065   GVA_StrongODR
00066 };
00067 
00068 inline bool isExternallyVisible(Linkage L) {
00069   return L == ExternalLinkage || L == VisibleNoLinkage;
00070 }
00071 
00072 inline Linkage getFormalLinkage(Linkage L) {
00073   if (L == UniqueExternalLinkage)
00074     return ExternalLinkage;
00075   if (L == VisibleNoLinkage)
00076     return NoLinkage;
00077   return L;
00078 }
00079 
00080 inline bool isExternalFormalLinkage(Linkage L) {
00081   return getFormalLinkage(L) == ExternalLinkage;
00082 }
00083 
00084 /// \brief Compute the minimum linkage given two linkages.
00085 ///
00086 /// The linkage can be interpreted as a pair formed by the formal linkage and
00087 /// a boolean for external visibility. This is just what getFormalLinkage and
00088 /// isExternallyVisible return. We want the minimum of both components. The
00089 /// Linkage enum is defined in an order that makes this simple, we just need
00090 /// special cases for when VisibleNoLinkage would lose the visible bit and
00091 /// become NoLinkage.
00092 inline Linkage minLinkage(Linkage L1, Linkage L2) {
00093   if (L2 == VisibleNoLinkage)
00094     std::swap(L1, L2);
00095   if (L1 == VisibleNoLinkage) {
00096     if (L2 == InternalLinkage)
00097       return NoLinkage;
00098     if (L2 == UniqueExternalLinkage)
00099       return NoLinkage;
00100   }
00101   return L1 < L2 ? L1 : L2;
00102 }
00103 
00104 } // end namespace clang
00105 
00106 #endif // LLVM_CLANG_BASIC_LINKAGE_H