ROSE 0.11.145.281
Snippet.h
1// Main documentation is in the "Snippet" class.
2
3#ifndef ROSE_AST_SNIPPET
4#define ROSE_AST_SNIPPET
5
6// rose.h and sage3basic.h should not be included in librose header files. [Robb P. Matzke 2014-10-15]
7// #include "sage3basic.h"
8#include "Map.h"
9
10#include <boost/shared_ptr.hpp>
11#include <boost/enable_shared_from_this.hpp>
12
13namespace Rose {
14
16typedef boost::shared_ptr<class SnippetFile> SnippetFilePtr;
17
19typedef boost::shared_ptr<class Snippet> SnippetPtr;
20
33
34std::ostream& operator<<(std::ostream&, const SnippetInsertion&);
35
51class SnippetFile: public boost::enable_shared_from_this<SnippetFile> {
52 friend class Snippet;
53private:
54 std::string fileName; // non-canonical source file name
55 SgSourceFile *ast; // AST corresponding to the file; null after parse errors
56
57 typedef Map<std::string/*functionName*/, std::vector<SgFunctionDefinition*> > FunctionDefinitionMap;
58 FunctionDefinitionMap functions; // cache the functions so we don't have to do a traversal every lookup
59
60 std::set<SgGlobal*> globals; // global scopes where this snippet has been inserted
61 std::map<std::string/*filename*/, std::set<SgGlobal*>/*insertion points*/> headersIncluded;
62
63 typedef Map<std::string/*fileName*/, SnippetFilePtr> Registry;
64 static Registry registry;
65
66 static std::vector<std::string> varNameList; // list of variable names to use when renaming things
67 bool copyAllSnippetDefinitions; // should all snippet definitions be copied to global scope?
68
69 Map<std::string, SgTypePtrList> blackListedDeclarations; // things we don't want to copy to the target AST
70
71 std::vector<SnippetInsertion> insertions; // list of everything we've inserted related to this snippet file
72
73protected:
75 explicit SnippetFile(const std::string &fileName, SgSourceFile *ast=NULL)
76 : fileName(fileName), ast(ast), copyAllSnippetDefinitions(false) {}
77
78public:
82 static SnippetFilePtr instance(const std::string &fileName, SgSourceFile *snippetAst=NULL);
83
86 static SnippetFilePtr lookup(const std::string &fileName);
87
89 const std::string& getName() const { return fileName; }
90
92 std::vector<std::string> getSnippetNames() const;
93
97 SnippetPtr findSnippet(const std::string &snippetName);
98
101 std::vector<SnippetPtr> findSnippets(const std::string &snippetName);
102
106
114 bool globallyInjected(SgGlobal *destination_scope);
115
117 bool fileIsIncluded(const std::string &filename, SgGlobal *destination_scope);
118
121 static size_t loadVariableNames(const std::string &fileName);
122
124 static std::string randomVariableName();
125
127 SgFile *getAst() const { return ast; }
128
133 bool getCopyAllSnippetDefinitions() const { return copyAllSnippetDefinitions; }
134 void setCopyAllSnippetDefinitions(bool b=true) { copyAllSnippetDefinitions = b; }
135 void clearCopyAllSnippetDefinitions() { copyAllSnippetDefinitions = false; }
141 void doNotInsert(const std::string &name, SgType *type=NULL);
142
145
147 const std::vector<SnippetInsertion>& getInsertedItems() const { return insertions; }
148
149protected:
152 static SgSourceFile* parse(const std::string &fileName);
153
156
158 void addInsertionRecord(const SnippetInsertion &inserted) { insertions.push_back(inserted); }
159};
160
161
162
311class Snippet {
312 friend class SnippetFile; // for protected constructor
313
314public:
323
334
335private:
336 typedef Map<SgSymbol*, SgNode*> ArgumentBindings; // bindings from snippet formals to actual vars and/or expressions
337 std::string name; // name of snippet
338 SnippetFilePtr file; // file containing the snippet definition
339 SgFunctionDefinition *ast; // snippet definition
340 InsertMechanism insertMechanism; // how snippet is inserted
341 LocalDeclarationPosition locDeclsPosition; // position for local declarations for INSERT_STMTS mode
342 bool insertRecursively; // is the insert() operation recursive?
343 bool fixupAst; // whether to fix up the target AST after inserting things
344
345protected:
346 // Use one of the "instance" methods instead.
347 Snippet(const std::string &name, const SnippetFilePtr &file, SgFunctionDefinition *ast)
348 : name(name), file(file), ast(ast), insertMechanism(INSERT_STMTS), locDeclsPosition(LOCDECLS_AT_END),
349 insertRecursively(true), fixupAst(true) {
350 assert(!name.empty());
351 assert(file!=NULL);
352 assert(ast!=NULL);
353 }
354
355public:
359 static SnippetPtr instance(const std::string &snippetName, const SnippetFilePtr &snippetFile);
360 static SnippetPtr instanceFromFile(const std::string &snippetName, const std::string &fileName);
364 const std::string& getName() const /*final*/ { return name; }
365
367 SnippetFilePtr getFile() const { return file; }
368
370 SgFunctionDefinition* getDefinition() const { return ast; }
371
373 size_t numberOfArguments() const;
374
377 InsertMechanism getInsertMechanism() const { return insertMechanism; }
378 void setInsertMechanism(InsertMechanism im) { insertMechanism = im; }
383 LocalDeclarationPosition getLocalDeclarationPosition() const { return locDeclsPosition; }
384 void setLocalDeclarationPosition(LocalDeclarationPosition pos) { locDeclsPosition = pos; }
391 bool getInsertRecursively() const { return insertRecursively; }
392 void setInsertRecursively(bool b=true) { insertRecursively = b; }
393 void clearInsertRecursively() { insertRecursively = false; }
400 void insert(SgStatement *insertionPoint);
401 void insert(SgStatement *insertionPoint, SgNode *arg1);
402 void insert(SgStatement *insertionPoint, SgNode *arg1, SgNode *arg2);
403 void insert(SgStatement *insertionPoint, SgNode *arg1, SgNode *arg2, SgNode *arg3);
404 void insert(SgStatement *insertionPoint, SgNode *arg1, SgNode *arg2, SgNode *arg3, SgNode *arg4);
405 void insert(SgStatement *insertionPoint, const std::vector<SgNode*> &args);
414 bool getFixupAst() const { return fixupAst; }
415 void setFixupAst(bool b=true) { fixupAst = b; }
416 void clearFixupAst() { fixupAst = false; }
419protected:
421 void causeUnparsing(SgNode *ast, Sg_File_Info *targetLocation);
422
424 static void replaceArguments(SgNode *ast, const ArgumentBindings&);
425
428
431
435 void insertRelatedThings(SgStatement *snippetInsertionPoint);
436
438 void insertRelatedThingsForJava(SgStatement *snippetInsertionPoint);
439
441 void insertRelatedThingsForC(SgStatement *snippetInsertionPoint);
442
446
447 // DQ (2/26/2014): Added functionality to address requirement to make snippet AST conform to expectations for any new transforamtion.
448 /* Fixup the AST fragement being inserted into the target AST to reset all possible references to the original snippet file. */
449 // void fixupSnippetInNewTargetAST(SgStatement *insertionPoint, SgStatement *toInsert);
450
451};
452
458public:
459 virtual ~SnippetAstTraversal() {}
460 void traverse(SgNode *ast);
461 virtual void operator()(SgNode*, AstSimpleProcessing::Order) = 0;
462};
463
464} // namespace
465#endif
Extends std::map with methods that return optional values.
Definition util/Map.h:16
Java-aware AST traversal.
Definition Snippet.h:457
Represents a source file containing related snippets.
Definition Snippet.h:51
bool globallyInjected(SgGlobal *destination_scope)
Indicates that global entities have been injected.
void setCopyAllSnippetDefinitions(bool b=true)
Accessor for the property that controls whether snippet definitions are copied into the global scope.
Definition Snippet.h:134
bool fileIsIncluded(const std::string &filename, SgGlobal *destination_scope)
Indicates that the specified file has been included into the specified scope.
void doNotInsert(const std::string &name, SgType *type=NULL)
Black list.
const std::vector< SnippetInsertion > & getInsertedItems() const
Information about things that have been inserted.
Definition Snippet.h:147
static std::string randomVariableName()
Return a random variable name.
const std::string & getName() const
Returns the name of the file.
Definition Snippet.h:89
std::vector< SnippetPtr > findSnippets(const std::string &snippetName)
Returns all snippets having the specified name.
void clearCopyAllSnippetDefinitions()
Accessor for the property that controls whether snippet definitions are copied into the global scope.
Definition Snippet.h:135
SgFile * getAst() const
Get the AST for the entire snippet file.
Definition Snippet.h:127
std::vector< std::string > getSnippetNames() const
Returns the list of snippet names.
static size_t loadVariableNames(const std::string &fileName)
Load variable names from a file.
SnippetPtr findSnippet(const std::string &snippetName)
Return a Snippet having the specified name.
static SnippetFilePtr instance(const std::string &fileName, SgSourceFile *snippetAst=NULL)
Constructor.
void expandSnippets(SgNode *ast)
Insert snippets in marked code.
void addInsertionRecord(const SnippetInsertion &inserted)
Add an insertion record.
Definition Snippet.h:158
bool getCopyAllSnippetDefinitions() const
Accessor for the property that controls whether snippet definitions are copied into the global scope.
Definition Snippet.h:133
SnippetFile(const std::string &fileName, SgSourceFile *ast=NULL)
Use instance() instead.
Definition Snippet.h:75
static SnippetFilePtr lookup(const std::string &fileName)
Look up the SnippetFile for this file name.
bool isBlackListed(SgDeclarationStatement *)
Return true if the declaration is black listed.
void findSnippetFunctions()
Find all snippet functions (they are the top-level function definitions) and add them to this Snippet...
static SgSourceFile * parse(const std::string &fileName)
Parse the snippet file.
Simple mechanism for inserting statements into a specimen.
Definition Snippet.h:311
InsertMechanism getInsertMechanism() const
Accessor for the snippet insertion mechanism.
Definition Snippet.h:377
void setFixupAst(bool b=true)
Determines whether the target AST should be fixed up after insertion.
Definition Snippet.h:415
bool getInsertRecursively() const
Accessor for the property that indicates whether an insert() should be recursive.
Definition Snippet.h:391
LocalDeclarationPosition
Determines where local declarations are injected when using INSERT_STMTS.
Definition Snippet.h:329
@ LOCDECLS_AT_CURSOR
Local declarations are not moved to a declarations area.
Definition Snippet.h:332
@ LOCDECLS_AT_BEGINNING
Local declarations inserted at beginning of function.
Definition Snippet.h:330
@ LOCDECLS_AT_END
Local declarations inserted at end of leading declarations.
Definition Snippet.h:331
void setInsertRecursively(bool b=true)
Accessor for the property that indicates whether an insert() should be recursive.
Definition Snippet.h:392
static SnippetPtr instanceFromFile(const std::string &snippetName, const std::string &fileName)
Construct a new Snippet.
static SnippetPtr instance(const std::string &snippetName, const SnippetFilePtr &snippetFile)
Construct a new Snippet.
SnippetFilePtr getFile() const
Return the file where this snippet is defined.
Definition Snippet.h:367
void setInsertMechanism(InsertMechanism im)
Accessor for the snippet insertion mechanism.
Definition Snippet.h:378
SgFunctionDefinition * getDefinition() const
Return the function definition for this snippet.
Definition Snippet.h:370
void insert(SgStatement *insertionPoint, const std::vector< SgNode * > &args)
Insert a snippet into the project.
InsertMechanism
Determines how a snippet is injected at the insertion point.
Definition Snippet.h:319
@ INSERT_BODY
Insert entire snippet body as a single scope.
Definition Snippet.h:320
@ INSERT_STMTS
Insert snippet statements one at a time.
Definition Snippet.h:321
void insert(SgStatement *insertionPoint, SgNode *arg1, SgNode *arg2)
Insert a snippet into the project.
void insertRelatedThingsForC(SgStatement *snippetInsertionPoint)
C-specific things that need to be copied from the snippet file to the target file.
bool getFixupAst() const
Determines whether the target AST should be fixed up after insertion.
Definition Snippet.h:414
void clearFixupAst()
Determines whether the target AST should be fixed up after insertion.
Definition Snippet.h:416
void insert(SgStatement *insertionPoint, SgNode *arg1, SgNode *arg2, SgNode *arg3, SgNode *arg4)
Insert a snippet into the project.
void insert(SgStatement *insertionPoint, SgNode *arg1, SgNode *arg2, SgNode *arg3)
Insert a snippet into the project.
static void removeIncludeDirectives(SgNode *)
Remove C preprocessor #include directives from the specified node.
static void replaceVariable(SgVarRefExp *, SgExpression *)
Replace a variable reference with some other expression.
void insert(SgStatement *insertionPoint)
Insert a snippet into the project.
static void replaceArguments(SgNode *ast, const ArgumentBindings &)
Replace formal argument occurrances with actual values.
void causeUnparsing(SgNode *ast, Sg_File_Info *targetLocation)
Mark nodes so they're unparsed when the insertion point is unparsed.
void renameTemporaries(SgNode *ast)
Rename snippet local variables so they don't interfere with names visible at the insertion point.
void insert(SgStatement *insertionPoint, SgNode *arg1)
Insert a snippet into the project.
const std::string & getName() const
Return the snippet name.
Definition Snippet.h:364
void setLocalDeclarationPosition(LocalDeclarationPosition pos)
Accessor for local declaration insertion position.
Definition Snippet.h:384
void insertRelatedThingsForJava(SgStatement *snippetInsertionPoint)
Java-specific things that need to be copied from the snippet file to the target file.
size_t numberOfArguments() const
Returns the number of formal arguments for the snippet.
void insertRelatedThings(SgStatement *snippetInsertionPoint)
Insert other things from the snippet file into the target file.
LocalDeclarationPosition getLocalDeclarationPosition() const
Accessor for local declaration insertion position.
Definition Snippet.h:383
void clearInsertRecursively()
Accessor for the property that indicates whether an insert() should be recursive.
Definition Snippet.h:393
This class represents the concept of a declaration statement.
This class represents the notion of an expression. Expressions are derived from SgLocatedNodes,...
This class represents a source file for a project (which may contian many source files and or directo...
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope,...
This class represents the concept of a namespace definition.
This class represents the base class for all IR nodes within Sage III.
This class represents the notion of a statement.
This class represents the base class for all types.
This class represents the variable refernece in expressions.
This class represents the location of the code associated with the IR node in the original source cod...
The ROSE library.
boost::shared_ptr< class Snippet > SnippetPtr
Shared-ownership pointer to a Snippet object.
Definition Snippet.h:19
boost::shared_ptr< class SnippetFile > SnippetFilePtr
Shared-ownership pointer to a SnippetFile object.
Definition Snippet.h:16
Information about something that was inserted.
Definition Snippet.h:24
SgNode * inserted
What was inserted.
Definition Snippet.h:25
SgNode * original
Copied from this node of the snippet file.
Definition Snippet.h:26
SgNode * insertedBefore
Inserted before this node in the target file.
Definition Snippet.h:27