Add experimental (and undocumented for now) export syntax.

This allows adding types to the list that are included in the automatically-generated
header files.

struct Foo { . . . };
struct Bar { . . . };

export { Foo, Bar };
This commit is contained in:
Matt Pharr
2012-06-05 12:51:21 -07:00
parent 96aaf6d53b
commit 592affb984
3 changed files with 64 additions and 0 deletions

View File

@@ -824,6 +824,22 @@ Module::AddFunctionDefinition(const std::string &name, const FunctionType *type,
}
void
Module::AddExportedTypes(const std::vector<std::pair<const Type *,
SourcePos> > &types) {
for (int i = 0; i < (int)types.size(); ++i) {
if (CastType<StructType>(types[i].first) == NULL &&
CastType<VectorType>(types[i].first) == NULL &&
CastType<EnumType>(types[i].first) == NULL)
Error(types[i].second, "Only struct, vector, and enum types, "
"not \"%s\", are allowed in type export lists.",
types[i].first->GetString().c_str());
else
exportedTypes.push_back(types[i]);
}
}
bool
Module::writeOutput(OutputType outputType, const char *outFileName,
const char *includeFileName) {
@@ -1251,6 +1267,18 @@ Module::writeHeader(const char *fn) {
lGetExportedParamTypes(externCFuncs, &exportedStructTypes,
&exportedEnumTypes, &exportedVectorTypes);
// Go through the explicitly exported types
for (int i = 0; i < (int)exportedTypes.size(); ++i) {
if (const StructType *st = CastType<StructType>(exportedTypes[i].first))
exportedStructTypes.push_back(st->GetAsUniformType());
else if (const EnumType *et = CastType<EnumType>(exportedTypes[i].first))
exportedEnumTypes.push_back(et->GetAsUniformType());
else if (const VectorType *vt = CastType<VectorType>(exportedTypes[i].first))
exportedVectorTypes.push_back(vt->GetAsUniformType());
else
FATAL("Unexpected type in export list");
}
// And print them
lEmitVectorTypedefs(exportedVectorTypes, f);
lEmitEnumDecls(exportedEnumTypes, f);

View File

@@ -80,6 +80,11 @@ public:
void AddFunctionDefinition(const std::string &name,
const FunctionType *ftype, Stmt *code);
/** Adds the given type to the set of types that have their definitions
included in automatically generated header files. */
void AddExportedTypes(const std::vector<std::pair<const Type *,
SourcePos> > &types);
/** After a source file has been compiled, output can be generated in a
number of different formats. */
enum OutputType { Asm, /** Generate text assembly language output */
@@ -145,6 +150,8 @@ private:
const char *filename;
AST *ast;
std::vector<std::pair<const Type *, SourcePos> > exportedTypes;
/** Write the corresponding output type to the given file. Returns
true on success, false if there has been an error. The given
filename may be NULL, indicating that output should go to standard

View File

@@ -151,6 +151,7 @@ struct ForeachDimension {
Expr *expr;
ExprList *exprList;
const Type *type;
std::vector<std::pair<const Type *, SourcePos> > *typeList;
const AtomicType *atomicType;
int typeQualifier;
StorageClass storageClass;
@@ -232,6 +233,7 @@ struct ForeachDimension {
%type <type> specifier_qualifier_list struct_or_union_specifier
%type <type> type_specifier type_name rate_qualified_type_specifier
%type <type> short_vec_specifier
%type <typeList> type_specifier_list
%type <atomicType> atomic_var_type_specifier
%type <typeQualifier> type_qualifier type_qualifier_list
@@ -826,6 +828,28 @@ type_specifier
| enum_specifier { $$ = $1; }
;
type_specifier_list
: type_specifier
{
if ($1 == NULL)
$$ = NULL;
else {
std::vector<std::pair<const Type *, SourcePos> > *vec =
new std::vector<std::pair<const Type *, SourcePos> >;
vec->push_back(std::make_pair($1, @1));
$$ = vec;
}
}
| type_specifier_list ',' type_specifier
{
$$ = $1;
if ($1 == NULL)
Assert(m->errorCount > 0);
else
$$->push_back(std::make_pair($3, @3));
}
;
atomic_var_type_specifier
: TOKEN_VOID { $$ = AtomicType::Void; }
| TOKEN_BOOL { $$ = AtomicType::UniformBool->GetAsUnboundVariabilityType(); }
@@ -1837,6 +1861,11 @@ translation_unit
external_declaration
: function_definition
| TOKEN_EXTERN TOKEN_STRING_C_LITERAL '{' declaration '}'
| TOKEN_EXPORT '{' type_specifier_list '}' ';'
{
if ($3 != NULL)
m->AddExportedTypes(*$3);
}
| declaration
{
if ($1 != NULL)