AtkRole
getRoleCB(AtkObject *aAtkObj)
{
  AccessibleWrap* accWrap = GetAccessibleWrap(aAtkObj);
  if (!accWrap)
    return ATK_ROLE_INVALID;

#ifdef DEBUG
  NS_ASSERTION(nsAccUtils::IsTextInterfaceSupportCorrect(accWrap),
      "Does not support nsIAccessibleText when it should");
#endif

  if (aAtkObj->role != ATK_ROLE_INVALID)
    return aAtkObj->role;

#define ROLE(geckoRole, stringRole, atkRole, macRole, msaaRole, ia2Role) \
  case roles::geckoRole: \
    aAtkObj->role = atkRole; \
    break;

  switch (accWrap->Role()) {
#include "RoleMap.h"
    default:
      MOZ_NOT_REACHED("Unknown role.");
      aAtkObj->role = ATK_ROLE_UNKNOWN;
  };

#undef ROLE

  return aAtkObj->role;
}
AtkRole
getRoleCB(AtkObject *aAtkObj)
{
  if (aAtkObj->role != ATK_ROLE_INVALID)
    return aAtkObj->role;

  AccessibleWrap* accWrap = GetAccessibleWrap(aAtkObj);
  a11y::role role;
  if (!accWrap) {
    ProxyAccessible* proxy = GetProxy(aAtkObj);
    if (!proxy)
      return ATK_ROLE_INVALID;

    role = proxy->Role();
  } else {
#ifdef DEBUG
    NS_ASSERTION(nsAccUtils::IsTextInterfaceSupportCorrect(accWrap),
                 "Does not support Text interface when it should");
#endif

    role = accWrap->Role();
  }

#define ROLE(geckoRole, stringRole, atkRole, macRole, \
             msaaRole, ia2Role, nameRule) \
  case roles::geckoRole: \
    aAtkObj->role = atkRole; \
    break;

  switch (role) {
#include "RoleMap.h"
    default:
      MOZ_CRASH("Unknown role.");
  };

#undef ROLE

  if (aAtkObj->role == ATK_ROLE_LIST_BOX && !IsAtkVersionAtLeast(2, 1))
    aAtkObj->role = ATK_ROLE_LIST;
  else if (aAtkObj->role == ATK_ROLE_TABLE_ROW && !IsAtkVersionAtLeast(2, 1))
    aAtkObj->role = ATK_ROLE_LIST_ITEM;
  else if (aAtkObj->role == ATK_ROLE_MATH && !IsAtkVersionAtLeast(2, 12))
    aAtkObj->role = ATK_ROLE_PANEL;
  else if (aAtkObj->role == ATK_ROLE_STATIC && !IsAtkVersionAtLeast(2, 16))
    aAtkObj->role = ATK_ROLE_TEXT;
  else if ((aAtkObj->role == ATK_ROLE_MATH_FRACTION ||
            aAtkObj->role == ATK_ROLE_MATH_ROOT) && !IsAtkVersionAtLeast(2, 16))
    aAtkObj->role = ATK_ROLE_UNKNOWN;

  return aAtkObj->role;
}
STDMETHODIMP
ia2Accessible::role(long* aRole)
{
  if (!aRole)
    return E_INVALIDARG;
  *aRole = 0;

  AccessibleWrap* acc = static_cast<AccessibleWrap*>(this);
  if (acc->IsDefunct())
    return CO_E_OBJNOTCONNECTED;

#define ROLE(_geckoRole, stringRole, atkRole, macRole, \
             msaaRole, ia2Role, androidClass, nameRule) \
  case roles::_geckoRole: \
    *aRole = ia2Role; \
    break;

  a11y::role geckoRole;
  MOZ_ASSERT(!acc->IsProxy());
  geckoRole = acc->Role();
  switch (geckoRole) {
#include "RoleMap.h"
    default:
      MOZ_CRASH("Unknown role.");
  }

#undef ROLE

  // Special case, if there is a ROLE_ROW inside of a ROLE_TREE_TABLE, then call
  // the IA2 role a ROLE_OUTLINEITEM.
  MOZ_ASSERT(!acc->IsProxy());
  if (geckoRole == roles::ROW) {
    Accessible* xpParent = acc->Parent();
    if (xpParent && xpParent->Role() == roles::TREE_TABLE)
      *aRole = ROLE_SYSTEM_OUTLINEITEM;
  }

  return S_OK;
}