Vuo 2.4.4
Loading...
Searching...
No Matches
VuoReplaceGenericTypesWithSpecializedAction.cc
Go to the documentation of this file.
1
12#include "VuoGenericType.hh"
13#include "VuoStringUtilities.hh"
14
15using namespace clang;
16
27 const map<string, string> &replacements,
28 bool shouldReplaceGenericTypesInVuoTypeAnnotations,
29 json_object *specializedModuleDetails,
30 unique_ptr<VuoPreprocessorCallbacks> preprocessorCallbacks) :
31 ASTFrontendAction(),
32 output(output),
33 replacements(replacements),
34 shouldReplaceGenericTypesInVuoTypeAnnotations(shouldReplaceGenericTypesInVuoTypeAnnotations),
35 specializedModuleDetails(specializedModuleDetails),
36 preprocessorCallbacks(std::move(preprocessorCallbacks))
37{
38}
39
43unique_ptr<ASTConsumer> VuoReplaceGenericTypesWithSpecializedAction::CreateASTConsumer(CompilerInstance &compilerInstance, llvm::StringRef inFile)
44{
45 rewriter.setSourceMgr(compilerInstance.getSourceManager(), compilerInstance.getLangOpts());
46 return unique_ptr<Consumer>(new Consumer(replacements, shouldReplaceGenericTypesInVuoTypeAnnotations, specializedModuleDetails, rewriter));
47}
48
53{
54 CI.getPreprocessor().addPPCallbacks(std::move(preprocessorCallbacks));
55 return true;
56}
57
62{
63 rewriter.getEditBuffer(rewriter.getSourceMgr().getMainFileID()).write(output);
64}
65
69VuoReplaceGenericTypesWithSpecializedAction::Consumer::Consumer(const map<string, string> &replacements,
70 bool shouldReplaceGenericTypesInVuoTypeAnnotations,
71 json_object *specializedModuleDetails,
72 clang::Rewriter &rewriter) :
73 visitor(replacements, shouldReplaceGenericTypesInVuoTypeAnnotations, specializedModuleDetails, rewriter)
74{
75}
76
80void VuoReplaceGenericTypesWithSpecializedAction::Consumer::HandleTranslationUnit(clang::ASTContext &context)
81{
82 visitor.TraverseDecl(context.getTranslationUnitDecl());
83}
84
88VuoReplaceGenericTypesWithSpecializedAction::Visitor::Visitor(const map<string, string> &replacements,
89 bool shouldReplaceGenericTypesInVuoTypeAnnotations,
90 json_object *specializedModuleDetails,
91 clang::Rewriter &rewriter) :
92 replacements(replacements),
93 shouldReplaceGenericTypesInVuoTypeAnnotations(shouldReplaceGenericTypesInVuoTypeAnnotations),
94 specializedModuleDetails(specializedModuleDetails),
95 rewriter(rewriter)
96{
97}
98
102bool VuoReplaceGenericTypesWithSpecializedAction::Visitor::VisitFunctionDecl(FunctionDecl *decl)
103{
104 DeclarationNameInfo nameInfo = decl->getNameInfo();
105 string name = nameInfo.getAsString();
106
107 if (replaceGenericTypeNamesInIdentifier(name))
108 rewriter.ReplaceText(nameInfo.getSourceRange(), name);
109
110 return true;
111}
112
117bool VuoReplaceGenericTypesWithSpecializedAction::Visitor::VisitDeclRefExpr(DeclRefExpr *expr)
118{
119 DeclarationNameInfo nameInfo = expr->getNameInfo();
120 string name = nameInfo.getAsString();
121 string originalName = name;
122
123 if (replaceGenericTypeNamesInIdentifier(name))
124 {
125 SourceLocation begin = expr->getLocStart();
126 SourceLocation end = begin.getLocWithOffset(originalName.length()-1);
127 SourceRange sourceRange(begin, end);
128 rewriter.ReplaceText(sourceRange, name);
129 }
130
131 return true;
132}
133
138bool VuoReplaceGenericTypesWithSpecializedAction::Visitor::VisitAnnotateAttr(AnnotateAttr *attr)
139{
140 if (! shouldReplaceGenericTypesInVuoTypeAnnotations)
141 return true;
142
143 string annotation = attr->getAnnotation().str();
144 if (VuoStringUtilities::beginsWith(annotation, "vuoType:"))
145 {
146 string attribute = rewriter.getRewrittenText(attr->getRange());
147 if (replaceGenericTypeNamesInIdentifier(attribute))
148 rewriter.ReplaceText(attr->getRange(), attribute);
149 }
150
151 return true;
152}
153
157bool VuoReplaceGenericTypesWithSpecializedAction::Visitor::VisitVarDecl(VarDecl *decl)
158{
159 if (! specializedModuleDetails)
160 return true;
161
162 if (decl->getNameAsString() == "moduleDetails" && decl->hasInit())
163 {
164 Expr *expr = decl->getInit();
165 while (isa<CastExpr>(expr))
166 expr = cast<CastExpr>(expr)->getSubExpr();
167
168 clang::StringLiteral *metadataExpr = dyn_cast<clang::StringLiteral>(expr);
169 if (metadataExpr)
170 {
171 json_object *metadataJson = json_tokener_parse(metadataExpr->getString().str().c_str());
172 json_object_object_add(metadataJson, "specializedModule", specializedModuleDetails);
173 string modifiedMetadata = string("VUO_STRINGIFY(") + json_object_to_json_string_ext(metadataJson, JSON_C_TO_STRING_PLAIN) + ")";
174
175 SourceLocation begin = metadataExpr->getLocStart();
176 SourceLocation end = metadataExpr->getLocEnd();
177 SourceRange sourceRange(begin, end);
178 rewriter.ReplaceText(sourceRange, modifiedMetadata);
179 }
180 }
181
182 return true;
183}
184
189bool VuoReplaceGenericTypesWithSpecializedAction::Visitor::replaceGenericTypeNamesInIdentifier(string &identifier)
190{
191 return VuoGenericType::replaceGenericTypeNamesInString(identifier, replacements, orderedGenericTypeNames);
192}