Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass a declarative table reference to a function #589

Merged
merged 3 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion production/tools/gaia_translate/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ void generate_table_subscription(const string& table, const string& field_subscr

if (g_is_rule_context_rule_name_referenced)
{
navigation_code.prefix.insert(0, "static const char gaia_rule_name[] = \"" + rule_name_log + "\";\n");
navigation_code.prefix.insert(0, "\nstatic const char gaia_rule_name[] = \"" + rule_name_log + "\";\n");
}
if (rule_count == 1)
{
Expand Down
24 changes: 21 additions & 3 deletions production/tools/gaia_translate/tests/test_mixed.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,31 @@ using namespace gaia::barn_storage;

int g_text_mixed_called = 0;

void f (gaia::barn_storage::incubator_t i)
daxhaw marked this conversation as resolved.
Show resolved Hide resolved
{
i.sensor_list().insert(sensor_t::insert_row("TestSensor1", 0, 0.0));
i.actuator_list().insert(actuator_t::insert_row("TestActuator1", 0, 0.0));
}

void f1 (const gaia::barn_storage::incubator_t& i)
{
i.sensor_list().insert(sensor_t::insert_row("TestSensor2", 0, 0.0));
i.actuator_list().insert(actuator_t::insert_row("TestActuator2", 0, 0.0));
}

void f2 (gaia::barn_storage::incubator_t& i)
{
i.sensor_list().insert(sensor_t::insert_row("TestSensor3", 0, 0.0));
i.actuator_list().insert(actuator_t::insert_row("TestActuator3", 0, 0.0));
}

ruleset test_mixed
{
OnInsert(incubator)
{
incubator_t incubator1 = incubator_t::get(incubator.gaia_id());
incubator1.sensor_list().insert(sensor_t::insert_row("TestSensor1", 0, 0.0));
incubator1.actuator_list().insert(actuator_t::insert_row("TestActuator1", 0, 0.0));
f(incubator);
f1(incubator);
f2(incubator);
g_text_mixed_called++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9518,4 +9518,6 @@ def err_invalid_rule_context_internal_error : Error<
"Internal error while generating 'rule_context'.">;
def err_invalid_rule_attribute : Error<
"Invalid Gaia rule attribute.">;
def err_ambiguous_field_name : Error<
"'%0' is ambiguous because both a table and a field with this name exist in the catalog.">;
} // end of sema component.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ QualType Sema::getTableType (IdentifierInfo *table, SourceLocation loc)
return Context.VoidTy;
}

const Type *realType = nullptr;

auto &types = Context.getTypes();
for (unsigned typeIdx = 0; typeIdx != types.size(); ++typeIdx)
{
const auto *type = types[typeIdx];
const RecordDecl *record = type->getAsRecordDecl();
if (record != nullptr)
{
const auto *id = record->getIdentifier();
if (id != nullptr)
{
if (id->getName().equals(tableName + "_t"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean that now all anchor references are of type table_t? If so does this mean I can call any method exposed by table_t?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean by "I can call any method exposed by table_t"?
Are you referring to the parameter type, like "I can call any function similar to f(table_t)"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you don't. I am not exposing methods of table_t type when you reference table in a rule. You can write f(table) and do inside the function whatever you want as function accepts real table_t type.

{
realType = type;
break;
}
}
}
}

RulesetDecl *rulesetDecl = dyn_cast<RulesetDecl>(c);
RulesetTableAttr *attr = rulesetDecl->getAttr<RulesetTableAttr>();

Expand Down Expand Up @@ -324,6 +345,24 @@ QualType Sema::getTableType (IdentifierInfo *table, SourceLocation loc)
AttributeFactory attrFactory;
ParsedAttributes attrs(attrFactory);


if (realType != nullptr)
{
QualType R = Context.getFunctionType(
BuildReferenceType(QualType(realType,0), true, loc, DeclarationName()),
None, FunctionProtoType::ExtProtoInfo());
CanQualType ClassType = Context.getCanonicalType(R);
DeclarationName Name = Context.DeclarationNames.getCXXConversionFunctionName(ClassType);
DeclarationNameInfo NameInfo(Name, loc);

auto conversionFunctionDeclaration = CXXConversionDecl::Create(
Context, cast<CXXRecordDecl>(RD), loc, NameInfo, R,
nullptr, false, false, false, SourceLocation());

conversionFunctionDeclaration->setAccess(AS_public);
RD->addDecl(conversionFunctionDeclaration);
}

for (const auto &f : tableDescription->second)
{
string fieldName = f.first;
Expand Down Expand Up @@ -353,6 +392,22 @@ QualType Sema::getFieldType (IdentifierInfo *id, SourceLocation loc)

std::string fieldName = fieldNameStrRef.str();

unordered_map<string, unordered_map<string, QualType>> tableData = getTableData(loc);

if (tableData.find(fieldName) != tableData.end())
{
for (auto iterator : tableData)
{
if (iterator.second.find(fieldName) != iterator.second.end())
{
Diag(loc,diag::err_ambiguous_field_name) << fieldName;
return Context.VoidTy;
}
}

return getTableType(id, loc);
}

DeclContext *c = getCurFunctionDecl();
while (c)
{
Expand All @@ -371,7 +426,6 @@ QualType Sema::getFieldType (IdentifierInfo *id, SourceLocation loc)
vector<string> tables;
RulesetDecl *rulesetDecl = dyn_cast<RulesetDecl>(c);
RulesetTableAttr * attr = rulesetDecl->getAttr<RulesetTableAttr>();
unordered_map<string, unordered_map<string, QualType>> tableData = getTableData(loc);

if (attr != nullptr)
{
Expand Down