Redefine fqname in hidl-gen_y.yy
There is an inconsistency in hidl-gen_y.yy that fqname does not
mean an FQName object, but a Type object. Redefined fqname to create
an FQName object only, and rename the original fqname rule to fqtype.
This also propagates to AST::lookupType and Scope::lookupType to
take an FQName object instead of a plain string.
Test: `mma`
Test: `make hidl_test && adb sync && adb shell hidl_test`
Change-Id: I5d35192fa5fa9752b10bd9e7d339eadc5cdb78c0
diff --git a/AST.cpp b/AST.cpp
index aa27a76..224387d 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -175,8 +175,7 @@
return true;
}
-Type *AST::lookupType(const char *name) {
- FQName fqName(name);
+Type *AST::lookupType(const FQName &fqName) {
CHECK(fqName.isValid());
if (fqName.name().empty()) {
@@ -188,7 +187,7 @@
// This is just a plain identifier, resolve locally first if possible.
for (size_t i = mScopePath.size(); i-- > 0;) {
- Type *type = mScopePath[i]->lookupType(name);
+ Type *type = mScopePath[i]->lookupType(fqName);
if (type != NULL) {
// Resolve typeDefs to the target type.
diff --git a/AST.h b/AST.h
index 2950a6f..1f9d4f0 100644
--- a/AST.h
+++ b/AST.h
@@ -66,7 +66,7 @@
// Look up a type by FQName, "pure" names, i.e. those without package
// or version are first looked up in the current scope chain.
// After that lookup proceeds to imports.
- Type *lookupType(const char *name);
+ Type *lookupType(const FQName &fqName);
void addImportedAST(AST *ast);
diff --git a/Scope.cpp b/Scope.cpp
index e7a48c6..9727ee6 100644
--- a/Scope.cpp
+++ b/Scope.cpp
@@ -47,8 +47,8 @@
return true;
}
-NamedType *Scope::lookupType(const char *name) const {
- auto it = mTypeIndexByName.find(name);
+NamedType *Scope::lookupType(const FQName &fqName) const {
+ auto it = mTypeIndexByName.find(fqName.string().c_str());
if (it != mTypeIndexByName.end()) {
return mTypes[it->second];
diff --git a/Scope.h b/Scope.h
index f6369c6..06a7b11 100644
--- a/Scope.h
+++ b/Scope.h
@@ -33,7 +33,7 @@
bool addType(NamedType *type, std::string *errorMsg);
- NamedType *lookupType(const char *name) const;
+ NamedType *lookupType(const FQName &fqName) const;
bool isScope() const override;
diff --git a/hidl-gen_y.yy b/hidl-gen_y.yy
index 6249886..65dfaee 100644
--- a/hidl-gen_y.yy
+++ b/hidl-gen_y.yy
@@ -94,7 +94,8 @@
%left UNARY_MINUS UNARY_PLUS '!' '~'
%type<str> optIdentifier package
-%type<type> fqname
+%type<fqName> fqname
+%type<type> fqtype
%type<type> type opt_storage_type
%type<type> enum_declaration
@@ -121,6 +122,7 @@
%union {
const char *str;
android::Type *type;
+ android::FQName *fqName;
android::CompoundType *compoundType;
android::CompoundField *field;
std::vector<android::CompoundField *> *fields;
@@ -219,20 +221,33 @@
fqname
: FQNAME
{
- $$ = ast->lookupType($1);
- if ($$ == NULL) {
- std::cerr << "ERROR: Failed to lookup type '" << $1 << "' at "
+ $$ = new FQName($1);
+ if(!$$->isValid()) {
+ std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
<< @1
- << "\n";
-
+ << ".\n";
YYERROR;
}
}
| IDENTIFIER
{
- $$ = ast->lookupType($1);
+ $$ = new FQName($1);
+ if(!$$->isValid()) {
+ std::cerr << "ERROR: FQName '" << $1 << "' is not valid at "
+ << @1
+ << ".\n";
+ YYERROR;
+ }
+ }
+ ;
+
+fqtype
+ : fqname
+ {
+ $$ = ast->lookupType(*($1));
if ($$ == NULL) {
- std::cerr << "Failed to lookup type '" << $1 << "' at " << @1
+ std::cerr << "ERROR: Failed to lookup type '" << $1->string() << "' at "
+ << @1
<< "\n";
YYERROR;
@@ -279,7 +294,7 @@
opt_extends
: /* empty */ { $$ = NULL; }
- | EXTENDS fqname { $$ = $2; }
+ | EXTENDS fqtype { $$ = $2; }
body
: opt_annotations INTERFACE IDENTIFIER opt_extends
@@ -507,7 +522,7 @@
opt_storage_type
: /* empty */ { $$ = NULL; }
- | ':' fqname
+ | ':' fqtype
{
$$ = $2;
@@ -591,8 +606,8 @@
;
type
- : fqname { $$ = $1; }
- | fqname '[' INTEGER ']'
+ : fqtype { $$ = $1; }
+ | fqtype '[' INTEGER ']'
{
if ($1->isBinder()) {
std::cerr << "ERROR: Arrays of interface types are not supported."
@@ -611,7 +626,7 @@
$$ = new ArrayType($1, size);
}
}
- | VEC '<' fqname '>'
+ | VEC '<' fqtype '>'
{
if ($3->isBinder()) {
std::cerr << "ERROR: Vectors of interface types are not "