2

I am currently developing a simple programming language for easily creating C++ projects. It lets you type in some short C++-like code and generates .h and .cpp files automatically.

I need some way to map a type, e.g. from the standard library, to its corresponding header file. That makes it possible to just use a type in my language and automatically infer which headers to include in the generated code.

Here is an example of the language as it is right now:

class Car {
    std::string print()

    members:
        int i, j
        std::array<Wheel, 4> wheels
}

When processing this code I find the types std::string and std::array which need to be mapped to their header files. How can I achieve this mapping?

Is there maybe a data base publicly available? I do not want to parse all the headers myself of course (which I assume is how IDEs do it).

On top of only supporting the standard library it would of course be useful to be able to support other libraries as well, but that is only a secondary goal.

4

1 回答 1

1

好吧,有一些优秀的参考资料可以用来构建自己的数据库。由于标准变化缓慢,您可以使用参考为它们生成地图。这是一个很好的C++ 参考

很难简单地在头文件中搜索,因为它们通常是使用您不想在代码中使用的后端位构建的,并且您无法区分它们。

如果我要构建它,我会使用标准库引用而不是通过头文件解析来构建基本映射;例如在上面的汽车示例中,您应该#include <string>对吗?

如果您要解析<string>给定编译器的头文件,您可能会发现它仅由后端位组成,而后端位又包含其他文件;

在 g++ 版本 4.6 中<string>看起来像这样:

// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
// 2005, 2006, 2007, 2009, 2010, 2011
// Free Software Foundation, Inc.
// GPL version 3.1 or later ... etc...

#ifndef _GLIBCXX_STRING
#define _GLIBCXX_STRING 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <bits/stringfwd.h>
#include <bits/char_traits.h>  // NB: In turn includes stl_algobase.h
#include <bits/allocator.h>
#include <bits/cpp_type_traits.h>
#include <bits/localefwd.h>    // For operators >>, <<, and getline.
#include <bits/ostream_insert.h>
#include <bits/stl_iterator_base_types.h>
#include <bits/stl_iterator_base_funcs.h>
#include <bits/stl_iterator.h>
#include <bits/stl_function.h> // For less
#include <ext/numeric_traits.h> 
#include <bits/stl_algobase.h> 
#include <bits/range_access.h>
#include <bits/basic_string.h>
#include <bits/basic_string.tcc> 

#endif /* _GLIBCXX_STRING */

现在问题变得很明显了。例如是<string>规范的std::ios_base吗?想了很多,以后可能会有其他的想法。

于 2013-05-18T14:24:32.260 回答