souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions
souffle::ast::transform::IOAttributesTransformer Class Reference

Transformation pass to set attribute names and types in IO operations. More...

#include <IOAttributes.h>

Inheritance diagram for souffle::ast::transform::IOAttributesTransformer:
Inheritance graph
Collaboration diagram for souffle::ast::transform::IOAttributesTransformer:
Collaboration graph

Public Member Functions

IOAttributesTransformerclone () const override
 
std::string getName () const override
 
- Public Member Functions inherited from souffle::ast::transform::Transformer
bool apply (TranslationUnit &translationUnit)
 
virtual ~Transformer ()=default
 

Private Member Functions

json11::Json getAlgebraicDataTypes (TranslationUnit &translationUnit) const
 Get sum types info for IO. More...
 
json11::Json getRecordsParams (TranslationUnit &translationUnit) const
 
json11::Json getRecordsTypes (TranslationUnit &translationUnit) const
 
std::string getRelationName (const Directive *node)
 
bool setAttributeNames (TranslationUnit &translationUnit)
 
bool setAttributeParams (TranslationUnit &translationUnit)
 
bool setAttributeTypes (TranslationUnit &translationUnit)
 
bool transform (TranslationUnit &translationUnit) override
 

Detailed Description

Transformation pass to set attribute names and types in IO operations.

Definition at line 60 of file IOAttributes.h.

Member Function Documentation

◆ clone()

IOAttributesTransformer* souffle::ast::transform::IOAttributesTransformer::clone ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 73 of file IOAttributes.h.

74  {
75  bool changed = false;

References souffle::ast::TranslationUnit::getAnalysis(), and souffle::ast::TranslationUnit::getProgram().

Here is the call graph for this function:

◆ getAlgebraicDataTypes()

json11::Json souffle::ast::transform::IOAttributesTransformer::getAlgebraicDataTypes ( TranslationUnit translationUnit) const
inlineprivate

Get sum types info for IO.

If they don't exists - create them.

The structure of JSON is approximately: {"ADTs" : {ADT_NAME : {"branches" : [branch..]}, {"arity": ...}}} branch = {{"types": [types ...]}, ["name": ...]}

Definition at line 202 of file IOAttributes.h.

202  {
203  auto& sumType =
204  dynamic_cast<const analysis::AlgebraicDataType&>(typeEnv.getType(astAlgebraicDataType));
205 
206  auto& branches = sumType.getBranches();
207 
208  std::vector<json11::Json> branchesInfo;
209 
210  for (const auto& branch : branches) {
211  std::vector<json11::Json> branchTypes;
212  for (auto* type : branch.types) {
213  branchTypes.push_back(getTypeQualifier(*type));
214  }
215 
216  auto branchInfo =
217  json11::Json::object{{{"types", std::move(branchTypes)}, {"name", branch.name}}};
218  branchesInfo.push_back(std::move(branchInfo));
219  }
220 
221  auto typeQualifier = analysis::getTypeQualifier(sumType);
222  auto&& sumInfo = json11::Json::object{{{"branches", std::move(branchesInfo)},
223  {"arity", static_cast<long long>(branches.size())}, {"enum", isADTEnum(sumType)}}};
224  sumTypes.emplace(std::move(typeQualifier), std::move(sumInfo));
225  });
226 
227  sumTypesInfo = json11::Json(sumTypes);
228  return sumTypesInfo;
229  }
230 
231  json11::Json getRecordsTypes(TranslationUnit& translationUnit) const {
232  static json11::Json ramRecordTypes;
233  // Check if the types where already constructed
234  if (!ramRecordTypes.is_null()) {
235  return ramRecordTypes;
236  }
237 
238  Program& program = translationUnit.getProgram();
239  auto typeEnv =
240  &translationUnit.getAnalysis<analysis::TypeEnvironmentAnalysis>()->getTypeEnvironment();
241  std::vector<std::string> elementTypes;
242  std::map<std::string, json11::Json> records;
243 

◆ getName()

std::string souffle::ast::transform::IOAttributesTransformer::getName ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 69 of file IOAttributes.h.

74  {

◆ getRecordsParams()

json11::Json souffle::ast::transform::IOAttributesTransformer::getRecordsParams ( TranslationUnit translationUnit) const
inlineprivate

Definition at line 278 of file IOAttributes.h.

280  : as<ast::RecordType>(astType)->getFields()) {
281  elementParams.push_back(field->getName());
282  }
283  const size_t recordArity = elementParams.size();
284  json11::Json recordInfo = json11::Json::object{
285  {"params", std::move(elementParams)}, {"arity", static_cast<long long>(recordArity)}};
286  records.emplace(astType->getQualifiedName().toString(), std::move(recordInfo));
287  }
288  }
289 
290  ramRecordParams = json11::Json(records);
291  return ramRecordParams;
292  }
293 };
294 
295 } // namespace souffle::ast::transform

◆ getRecordsTypes()

json11::Json souffle::ast::transform::IOAttributesTransformer::getRecordsTypes ( TranslationUnit translationUnit) const
inlineprivate

Definition at line 245 of file IOAttributes.h.

245  : program.getTypes()) {
246  const auto& type = typeEnv->getType(*astType);
247  if (isA<analysis::RecordType>(type)) {
248  elementTypes.clear();
249 
250  for (const analysis::Type* field : as<analysis::RecordType>(type)->getFields()) {
251  elementTypes.push_back(getTypeQualifier(*field));
252  }
253  const size_t recordArity = elementTypes.size();
254  json11::Json recordInfo = json11::Json::object{
255  {"types", std::move(elementTypes)}, {"arity", static_cast<long long>(recordArity)}};
256  records.emplace(getTypeQualifier(type), std::move(recordInfo));
257  }
258  }
259 
260  ramRecordTypes = json11::Json(records);
261  return ramRecordTypes;
262  }
263 
264  json11::Json getRecordsParams(TranslationUnit& translationUnit) const {
265  static json11::Json ramRecordParams;
266  // Check if the types where already constructed
267  if (!ramRecordParams.is_null()) {
268  return ramRecordParams;
269  }
270 
271  Program& program = translationUnit.getProgram();
272  std::vector<std::string> elementParams;
273  std::map<std::string, json11::Json> records;
274 
275  // Iterate over all record types in the program populating the records map.
276  for (auto* astType : program.getTypes()) {

References souffle::ast::analysis::getTypeQualifier().

Here is the call graph for this function:

◆ getRelationName()

std::string souffle::ast::transform::IOAttributesTransformer::getRelationName ( const Directive node)
inlineprivate

Definition at line 190 of file IOAttributes.h.

192  {

◆ setAttributeNames()

bool souffle::ast::transform::IOAttributesTransformer::setAttributeNames ( TranslationUnit translationUnit)
inlineprivate

Definition at line 121 of file IOAttributes.h.

124  : rel->getAttributes()) {
125  attributeNames.push_back(attribute->getName());
126  }
127 
128  if (Global::config().has("provenance")) {
129  auto auxArityAnalysis = translationUnit.getAnalysis<analysis::AuxiliaryArityAnalysis>();
130  std::vector<std::string> originalAttributeNames(
131  attributeNames.begin(), attributeNames.end() - auxArityAnalysis->getArity(rel));
132  io->addParameter("attributeNames", toString(join(originalAttributeNames, delimiter)));
133  } else {
134  io->addParameter("attributeNames", toString(join(attributeNames, delimiter)));
135  }
136  changed = true;
137  }
138  return changed;
139  }
140 
141  bool setAttributeTypes(TranslationUnit& translationUnit) {
142  bool changed = false;
143  Program& program = translationUnit.getProgram();
144  auto auxArityAnalysis = translationUnit.getAnalysis<analysis::AuxiliaryArityAnalysis>();
145  auto typeEnv =
146  &translationUnit.getAnalysis<analysis::TypeEnvironmentAnalysis>()->getTypeEnvironment();
147 
148  for (Directive* io : program.getDirectives()) {
149  Relation* rel = getRelation(program, io->getQualifiedName());
150  // Prepare type system information.
151  std::vector<std::string> attributesTypes;
152 
153  for (const auto* attribute : rel->getAttributes()) {

◆ setAttributeParams()

bool souffle::ast::transform::IOAttributesTransformer::setAttributeParams ( TranslationUnit translationUnit)
inlineprivate

Definition at line 88 of file IOAttributes.h.

92  {static_cast<long long>(rel->getArity() - auxArityAnalysis->getArity(rel))};
93  long long auxArity{static_cast<long long>(auxArityAnalysis->getArity(rel))};
94 
95  json11::Json relJson = json11::Json::object{{"arity", arity}, {"auxArity", auxArity},
96  {"params", json11::Json::array(attributesParams.begin(), attributesParams.end())}};
97 
99  {"relation", relJson}, {"records", getRecordsParams(translationUnit)}};
100 
101  io->addParameter("params", params.dump());
102  changed = true;
103  }
104  return changed;
105  }
106 
107  bool setAttributeNames(TranslationUnit& translationUnit) {
108  bool changed = false;
109  Program& program = translationUnit.getProgram();
110  for (Directive* io : program.getDirectives()) {
111  if (io->getType() == ast::DirectiveType::limitsize) {
112  continue;
113  }
114  if (io->hasParameter("attributeNames")) {
115  continue;
116  }
117  Relation* rel = getRelation(program, io->getQualifiedName());
118  std::string delimiter("\t");
119  if (io->hasParameter("delimiter")) {

◆ setAttributeTypes()

bool souffle::ast::transform::IOAttributesTransformer::setAttributeTypes ( TranslationUnit translationUnit)
inlineprivate

Definition at line 155 of file IOAttributes.h.

160  {static_cast<long long>(rel->getArity() - auxArityAnalysis->getArity(rel))};
161  long long auxArity{static_cast<long long>(auxArityAnalysis->getArity(rel))};
162 
163  json11::Json relJson = json11::Json::object{{"arity", arity}, {"auxArity", auxArity},
164  {"types", json11::Json::array(attributesTypes.begin(), attributesTypes.end())}};
165 
167  json11::Json::object{{"relation", relJson}, {"records", getRecordsTypes(translationUnit)},
168  {"ADTs", getAlgebraicDataTypes(translationUnit)}};
169 
170  io->addParameter("types", types.dump());
171  changed = true;
172  }
173  return changed;
174  }
175 
176  std::string getRelationName(const Directive* node) {
177  return toString(join(node->getQualifiedName().getQualifiers(), "."));
178  }
179 
180  /**
181  * Get sum types info for IO.
182  * If they don't exists - create them.
183  *
184  * The structure of JSON is approximately:
185  * {"ADTs" : {ADT_NAME : {"branches" : [branch..]}, {"arity": ...}}}
186  * branch = {{"types": [types ...]}, ["name": ...]}
187  */
188  json11::Json getAlgebraicDataTypes(TranslationUnit& translationUnit) const {

◆ transform()

bool souffle::ast::transform::IOAttributesTransformer::transform ( TranslationUnit translationUnit)
inlineoverrideprivatevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 78 of file IOAttributes.h.

79  : program.getDirectives()) {
80  if (io->getType() == ast::DirectiveType::limitsize) {
81  continue;
82  }
83  Relation* rel = getRelation(program, io->getQualifiedName());
84  // Prepare type system information.
85  std::vector<std::string> attributesParams;
86 

References souffle::ast::getRelation(), souffle::ast::limitsize, and rel().

Here is the call graph for this function:

The documentation for this class was generated from the following file:
souffle::ast::transform::IOAttributesTransformer::getRecordsTypes
json11::Json getRecordsTypes(TranslationUnit &translationUnit) const
Definition: IOAttributes.h:245
json11::Json::dump
void dump(std::string &out) const
Definition: json11.h:370
souffle::ast::analysis::isADTEnum
bool isADTEnum(const AlgebraicDataType &type)
Determine if ADT is enumerations (are all constructors empty)
Definition: TypeSystem.cpp:377
souffle::ast::transform::IOAttributesTransformer::setAttributeNames
bool setAttributeNames(TranslationUnit &translationUnit)
Definition: IOAttributes.h:121
types
std::vector< Own< ast::Type > > types
Definition: ComponentInstantiation.cpp:64
json11::Json::object
std::map< std::string, Json > object
Definition: json11.h:94
souffle::toString
const std::string & toString(const std::string &str)
A generic function converting strings into strings (trivial case).
Definition: StringUtil.h:234
souffle::ast::transform::IOAttributesTransformer::setAttributeTypes
bool setAttributeTypes(TranslationUnit &translationUnit)
Definition: IOAttributes.h:155
souffle::ast::getRelation
Relation * getRelation(const Program &program, const QualifiedName &name)
Returns the relation with the given name in the program.
Definition: Utils.cpp:101
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
json11::Json::array
std::vector< Json > array
Definition: json11.h:93
json11::Json
Definition: json11.h:87
souffle::Global::config
static MainConfig & config()
Definition: Global.h:141
json11::Json::is_null
bool is_null() const
Definition: json11.h:135
souffle::ast::transform::IOAttributesTransformer::getRecordsParams
json11::Json getRecordsParams(TranslationUnit &translationUnit) const
Definition: IOAttributes.h:278
souffle::ast::transform::IOAttributesTransformer::getAlgebraicDataTypes
json11::Json getAlgebraicDataTypes(TranslationUnit &translationUnit) const
Get sum types info for IO.
Definition: IOAttributes.h:202
souffle::ast::DirectiveType::limitsize
@ limitsize
souffle::ast::analysis::getTypeQualifier
std::string getTypeQualifier(const Type &type)
Returns full type qualifier for a given type.
Definition: TypeSystem.cpp:204
souffle::ast::transform::IOAttributesTransformer::getRelationName
std::string getRelationName(const Directive *node)
Definition: IOAttributes.h:190
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
std::type
ElementType type
Definition: span.h:640