将函数名称和属性存储在哈希表中

Store function name with attributes in hash table

本文关键字:存储 哈希表 属性 函数      更新时间:2023-10-16

假设我有以下程序:

__attribute__((annotate("ex"))) void ex() {};
void val() {};
__attribute__((annotate("foo"))) void foo() {
 ex();
cout<<"You have called a function with an attribute"<<endl;
val();
cerr<<"Error!You have called a function without an attribute"<<endl;
}
int main()
{
foo();
return 0;
}
如果你观察,main() 调用 foo() 和

foo() 依次调用 ex() 和 val()。当调用 ex() 时没有问题,因为它有一个与之关联的属性,但是当它调用 val() 时,它应该显示错误消息,说明此函数没有任何属性。

因此,为此,我需要跟踪哈希表中具有属性和没有属性的函数,并在调用函数时对其进行检查。

而且,我不知道该怎么做?我希望现在清楚了。

看起来你需要使用结构

我将给你一个参数的看法,你可以扩展到一个函数。

函数参数具有名称和类型

struct Function_Parameter
{
  std::string name;
  std::string type;
};

函数还具有名称和返回类型:

struct Function_Token
{
  std::string name;
  std::string return_type;
};

一个函数可以有零个或多个参数。 因此通常意味着某种容器。 因此,请使用std::vector

struct Function_Token
{
  std::string name;
  std::string return_type;
  std::vector<Function_Parameter> parameters;
};

许多编译器可能会将名称转换为数值(使用某种哈希函数或表)。 这通常会加快编译速度,因为字符串需要很长时间才能比较。