友情功能如何在内部实施

时间:2015-04-14 10:43:48

标签: c++ compiler-construction friend-function

我们在C ++中都使用了全局级别以及类级别的友元函数。 我试图在互联网上搜索内部朋友功能是如何实现的。

"朋友"做了什么操作?关键词。 例如,我们知道如何在内部实现v-ptr和v-table,我正在寻找同样的答案。

请注意: 此问题与朋友功能或朋友功能的使用无关。

1 个答案:

答案 0 :(得分:5)

出于这个答案的目的,我在gcc上搜索了一个非常旧的版本,因为它更小,更容易推理。在最新版本或其他编译器中实现可能完全不同。

可以在opensource.apple.com

上查看搜索到的源文件

gcc有一个函数is_friend(),在 friend.c 中定义,如果函数或类型是朋友,它基本上会返回。以下是函数的相关代码:

int
is_friend (type, supplicant)
     tree type, supplicant;
{
  int declp;
  register tree list;
  tree context;

  if (supplicant == NULL_TREE || type == NULL_TREE)
    return 0;
/*
Comment added by me: The following defines are in tree.h
#define TREE_CODE(NODE) ((enum tree_code) (NODE)->common.code)
#define TREE_CODE_CLASS(CODE)    tree_code_type[(int) (CODE)]
This is expanded as:
declp = (tree_code_type[(int) (((enum tree_code) (supplicant)->common.code))] == 'd')
*/
declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
// That is, it will simply search the array for the code of the supplicant and check if it is a function declaration.

  if (declp)
    /* It's a function decl.  */
    {
      tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type));
      tree name = DECL_NAME (supplicant);
      tree ctype;

      if (DECL_FUNCTION_MEMBER_P (supplicant))
    ctype = DECL_CLASS_CONTEXT (supplicant);
      else
    ctype = NULL_TREE;

      for (; list ; list = TREE_CHAIN (list))
    {
      if (name == FRIEND_NAME (list))
        {
          tree friends = FRIEND_DECLS (list);
          for (; friends ; friends = TREE_CHAIN (friends))
        {
          if (same_type_p (ctype, TREE_PURPOSE (friends)))
            return 1;

          if (TREE_VALUE (friends) == NULL_TREE)
            continue;

          if (supplicant == TREE_VALUE (friends))
            return 1;

          /* With -fguiding-decls we are more lenient about
             friendship.  This is bogus in general since two
             specializations of a template with non-type
             template parameters may have the same type, but
             be different.  

             Temporarily, we are also more lenient to deal
             with nested friend functions, for which there can
             be more than one FUNCTION_DECL, despite being the
             same function.  When that's fixed, the
             FUNCTION_MEMBER_P bit can go.  */
          if ((flag_guiding_decls 
               || DECL_FUNCTION_MEMBER_P (supplicant))
              && same_type_p (TREE_TYPE (supplicant),
                      TREE_TYPE (TREE_VALUE (friends))))
            return 1;

          if (TREE_CODE (TREE_VALUE (friends)) == TEMPLATE_DECL
              && is_specialization_of (supplicant, 
                           TREE_VALUE (friends)))
            return 1;
        }
          break;
        }
    }
    }
  else
    /* It's a type.  */
    {
    // ...
    }

  if (declp && DECL_FUNCTION_MEMBER_P (supplicant))
    context = DECL_CLASS_CONTEXT (supplicant);
  else if (! declp)
    /* Local classes have the same access as the enclosing function.  */
    context = hack_decl_function_context (TYPE_MAIN_DECL (supplicant));
  else
    context = NULL_TREE;

  /* A namespace is not friend to anybody. */
  if (context && TREE_CODE (context) == NAMESPACE_DECL)
    context = NULL_TREE;

  if (context)
    return is_friend (type, context);

  return 0;
}

基本上,它获取特定类型的朋友列表,并迭代它,检查它们中的任何一个是否等于被测试的函数。 通过使用在同一源文件中定义的类似函数将Friend函数添加到类型中:add_friend()add_friends()用于类的所有成员函数,make_friend_class()用于类等。

我猜测它使用此函数来确定,在检查访问时(如果该功能有访问权限,它可能会对您进行访问私有成员的那一刻),

你的问题的答案是(至少对于旧版本的GCC):编译器有每个类的朋友列表,并迭代此列表以确定函数是否是朋友(在编译时,课程。)。如果是,它只是授予访问权限。没有为此生成特殊代码:它是常规函数调用。