HLSL: Implement basic "struct" grammar.

This commit is contained in:
John Kessenich
2016-06-11 16:43:14 -06:00
parent 41ebc42926
commit e6e7494e2a
8 changed files with 257 additions and 6 deletions

View File

@@ -210,6 +210,22 @@ void HlslGrammar::acceptQualifier(TQualifier& qualifier)
bool HlslGrammar::acceptType(TType& type)
{
switch (peek()) {
case EHTokStruct:
return acceptStruct(type);
break;
case EHTokIdentifier:
// An identifier could be for a user-defined type.
// Note we cache the symbol table lookup, to save for a later rule
// when this is not a type.
token.symbol = parseContext.symbolTable.find(*token.string);
if (token.symbol && token.symbol->getAsVariable() && token.symbol->getAsVariable()->isUserType()) {
type.shallowCopy(token.symbol->getType());
advanceToken();
return true;
} else
return false;
case EHTokVoid:
new(&type) TType(EbtVoid);
break;
@@ -554,6 +570,125 @@ bool HlslGrammar::acceptType(TType& type)
return true;
}
// struct
// : STRUCT IDENTIFIER LEFT_BRACE struct_declaration_list RIGHT_BRACE
// | STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE
//
bool HlslGrammar::acceptStruct(TType& type)
{
// STRUCT
if (! acceptTokenClass(EHTokStruct))
return false;
// IDENTIFIER
TString structName = "";
if (peekTokenClass(EHTokIdentifier)) {
structName = *token.string;
advanceToken();
}
// LEFT_BRACE
if (! acceptTokenClass(EHTokLeftBrace)) {
expected("{");
return false;
}
// struct_declaration_list
TTypeList* typeList;
if (! acceptStructDeclarationList(typeList)) {
expected("struct member declarations");
return false;
}
// RIGHT_BRACE
if (! acceptTokenClass(EHTokRightBrace)) {
expected("}");
return false;
}
// create the user-defined type
new(&type) TType(typeList, structName);
// If it was named, which means it can be reused later, add
// it to the symbol table.
if (structName.size() > 0) {
TVariable* userTypeDef = new TVariable(&structName, type, true);
if (! parseContext.symbolTable.insert(*userTypeDef))
parseContext.error(token.loc, "redefinition", structName.c_str(), "struct");
}
return true;
}
// struct_declaration_list
// : struct_declaration SEMI_COLON struct_declaration SEMI_COLON ...
//
// struct_declaration
// : fully_specified_type struct_declarator COMMA struct_declarator ...
//
// struct_declarator
// : IDENTIFIER
// | IDENTIFIER array_specifier
//
bool HlslGrammar::acceptStructDeclarationList(TTypeList*& typeList)
{
typeList = new TTypeList();
do {
// success on seeing the RIGHT_BRACE coming up
if (peekTokenClass(EHTokRightBrace))
return true;
// struct_declaration
// fully_specified_type
TType memberType;
if (! acceptFullySpecifiedType(memberType)) {
expected("member type");
return false;
}
// struct_declarator COMMA struct_declarator ...
do {
// peek IDENTIFIER
if (! peekTokenClass(EHTokIdentifier)) {
expected("member name");
return false;
}
// add it to the list of members
TTypeLoc member = { new TType(EbtVoid), token.loc };
member.type->shallowCopy(memberType);
member.type->setFieldName(*token.string);
typeList->push_back(member);
// accept IDENTIFIER
advanceToken();
// array_specifier
// TODO
// success on seeing the SEMICOLON coming up
if (peekTokenClass(EHTokSemicolon))
break;
// COMMA
if (! acceptTokenClass(EHTokComma)) {
expected(",");
return false;
}
} while (true);
// SEMI_COLON
if (! acceptTokenClass(EHTokSemicolon)) {
expected(";");
return false;
}
} while (true);
}
// function_parameters
// : LEFT_PAREN parameter_declaration COMMA parameter_declaration ... RIGHT_PAREN
// | LEFT_PAREN VOID RIGHT_PAREN
@@ -879,7 +1014,7 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
} else if (acceptIdentifier(idToken)) {
// identifier or function_call name
if (! peekTokenClass(EHTokLeftParen)) {
node = parseContext.handleVariable(idToken.loc, token.string);
node = parseContext.handleVariable(idToken.loc, idToken.symbol, token.string);
} else if (acceptFunctionCall(idToken, node)) {
// function_call (nothing else to do yet)
} else {

View File

@@ -61,6 +61,8 @@ namespace glslang {
bool acceptFullySpecifiedType(TType&);
void acceptQualifier(TQualifier&);
bool acceptType(TType&);
bool acceptStruct(TType&);
bool acceptStructDeclarationList(TTypeList*&);
bool acceptFunctionParameters(TFunction&);
bool acceptParameterDeclaration(TFunction&);
bool acceptFunctionDefinition(TFunction&, TIntermNode*&);

View File

@@ -282,10 +282,14 @@ void C_DECL HlslParseContext::ppWarn(const TSourceLoc& loc, const char* szReason
//
// Handle seeing a variable identifier in the grammar.
//
TIntermTyped* HlslParseContext::handleVariable(const TSourceLoc& loc, const TString* string)
TIntermTyped* HlslParseContext::handleVariable(const TSourceLoc& loc, TSymbol* symbol, const TString* string)
{
TSymbol* symbol = symbolTable.find(*string);
TIntermTyped* node = nullptr;
if (symbol == nullptr)
symbol = symbolTable.find(*string);
if (symbol && symbol->getAsVariable() && symbol->getAsVariable()->isUserType()) {
error(loc, "expected symbol, not user-defined type", string->c_str(), "");
return nullptr;
}
// Error check for requiring specific extensions present.
if (symbol && symbol->getNumExtensions())
@@ -306,6 +310,7 @@ TIntermTyped* HlslParseContext::handleVariable(const TSourceLoc& loc, const TStr
const TVariable* variable;
const TAnonMember* anon = symbol ? symbol->getAsAnonMember() : nullptr;
TIntermTyped* node = nullptr;
if (anon) {
// It was a member of an anonymous container.

View File

@@ -65,7 +65,7 @@ public:
bool builtInName(const TString&);
void handlePragma(const TSourceLoc&, const TVector<TString>&);
TIntermTyped* handleVariable(const TSourceLoc&, const TString* string);
TIntermTyped* handleVariable(const TSourceLoc&, TSymbol* symbol, const TString* string);
TIntermTyped* handleBracketDereference(const TSourceLoc&, TIntermTyped* base, TIntermTyped* index);
void checkIndex(const TSourceLoc&, const TType&, int& index);

View File

@@ -54,7 +54,7 @@ class TPpToken;
// Everything needed to fully describe a token.
//
struct HlslToken {
HlslToken() : string(nullptr) { loc.init(); }
HlslToken() : string(nullptr), symbol(nullptr) { loc.init(); }
TSourceLoc loc; // location of token in the source
EHlslTokenClass tokenClass; // what kind of token it is
union { // what data the token holds
@@ -64,6 +64,7 @@ struct HlslToken {
bool b;
double d;
};
glslang::TSymbol* symbol; // if a symbol-table lookup was done already, this is the result
};
//