ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rose_attributes_list.h
Go to the documentation of this file.
1 #ifndef __ROSEAttributesList_H__
2 #define __ROSEAttributesList_H__
3 
4 //#include "setup.h"
5 
6 //#include <list>
7 //#include <vector>
8 #include <map>
9 
10 // Include the ROSE lex specific definitions of tokens
11 #include "general_token_defs.h"
12 
13 // #ifdef CAN_NOT_COMPILE_WITH_ROSE
14 // #warning "CAN_NOT_COMPILE_WITH_ROSE IS defined"
15 // #else
16 // #warning "CAN_NOT_COMPILE_WITH_ROSE is NOT defined"
17 // #endif
18 
19 // DQ (2/28/2010): Skip this if we are compiling ROSE using ROSE.
20 // This is being used in place of the CAN_NOT_COMPILE_WITH_ROSE macro.
21 // Note that CAN_NOT_COMPILE_WITH_ROSE is set by the following projects:
22 // 1) projects/DocumentationGenerator
23 // 2) projects/haskellport
24 // in their Makefile.am files. I think that using CXX_IS_ROSE_ANALYSIS
25 // will be equivalent (used to indicate the a ROSE translator is being
26 // used to compile the ROSE source code).
27 // However, it might be that this is equivalent to the USE_ROSE macro,
28 // which is set for all ROSE translators when they compile any code.
29 // DQ (12/22/2008): I would appreciate it if this were a better name...
30 // #if !CAN_NOT_COMPILE_WITH_ROSE
31 // #ifndef USE_ROSE
32 
33 // DQ (5/21/2010): I have built a separate macro for tuning off the compilation of WAVE
34 // since it is done only for the purpose of allowing ROSE based projects:
35 // 1) projects/DocumentationGenerator
36 // 2) projects/haskellport
37 // to process the ROSE files cleanly. ROSE can however process and compile ROSE
38 // (including a slightly modified version of WAVE that EDG will accept) and we
39 // separately test this in noightly tests. The previous 5/18/2010 fix to permit
40 // the Haskell support to skip processign WAVE turned this off and broke the nightly
41 // tests of ROSE compiling ROSE. This use of the macro ROSE_SKIP_COMPILATION_OF_WAVE
42 // it menat to be turned on by ROSE based tools that need to process the ROSE code
43 // and which currently fail (because of Wave) and so turn of the processing of Wave
44 // for those tools.
45 // #ifdef USE_ROSE
46 // #define ROSE_SKIP_COMPILATION_OF_WAVE
47 // #endif
48 
49 #ifndef ROSE_SKIP_COMPILATION_OF_WAVE
50  #if _MSC_VER < 1600 // 1600 == VC++ 10.0
51  #include <boost/preprocessor/iteration/iterate.hpp> // Liao, 7/10/2009, required by GCC 4.4.0 for a #define line of BOOST_PP_ITERATION_DEPTH
52  #ifdef _MSC_VER
53  #include <boost/wave.hpp> // CH (4/7/2010): Put this header here to avoid compiling error about mismatch between defination and declaration
54  #endif
55  #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
56  #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer type
57  #else
58 // #warning "Setting CAN_NOT_COMPILE_WITH_ROSE to value = 1"
59 // #define CAN_NOT_COMPILE_WITH_ROSE 1
60 // tps (12/4/2009) : This is not found in VC++ 10.0 and Boost 1.4
61  #pragma message ("Boost preprocessor and wave not included yet for VC++ 10.0")
62 
63  #endif
64 #endif
65 
66 //template boost::wave::cpplexer::impl::token_data<std::string, boost::wave::util::file_position_type>::delete(std::size_t) ;
67 // DQ (10/16/2002): Required for compiling with SUN 5.2 C++ compiler
68 #ifndef NAMESPACE_IS_BROKEN
69 // DQ (12/30/2005): This is a Bad Bad thing to do (I can explain)
70 // it hides names in the global namespace and causes errors in
71 // otherwise valid and useful code. Where it is needed it should
72 // appear only in *.C files (and only ones not included for template
73 // instantiation reasons) else they effect user who use ROSE unexpectedly.
74 // using namespace std;
75 #endif
76 
78 class ROSEAttributesList;
79 //AS(01/04/07) Global map of filenames to PreprocessingInfo*'s as it is inefficient
80 //to get this by a traversal of the AST
81 extern std::map<std::string,ROSEAttributesList* > mapFilenameToAttributes;
82 
83 
84 // DQ (4/19/2006): Forward declaration so that PreprocessingInfo can
85 // contain a pointer to a Sg_File_Info object.
86 class Sg_File_Info;
87 
88 // DQ (1/21/2008): Need forward declaration
89 class SgFile;
90 
91 // #if !CAN_NOT_COMPILE_WITH_ROSE
92 // #ifndef USE_ROSE
93 #ifndef ROSE_SKIP_COMPILATION_OF_WAVE
94 
95 typedef boost::wave::cpplexer::lex_token<> token_type;
96 typedef std::vector<token_type> token_container;
97 typedef std::list<token_type> token_list_container;
98 typedef std::vector<std::list<token_type> > token_container_container;
99 
100 #endif
101 
104  {
105  public:
106  // DQ (10/15/2002) moved this to nested scope to avoid global name pollution :-).
110  {
111  defaultValue = 0, // let the zero value be an error value
112  undef = 1, // Position of the directive is only going to be defined
113  // when the preprocessing object is copied into the AST,
114  // it remains undefined before that
115  before = 2, // Directive goes before the correponding code segment
116  after = 3, // Directive goes after the correponding code segment
117  inside = 4, // Directive goes inside the correponding code segment (as in between "{" and "}" of an empty basic block)
118 
119  // DQ (7/19/2008): Added additional fields so that we could use this enum type in the AstUnparseAttribute
120  // replace = 5, // Support for replacing the IR node in the unparsing of any associated subtree
121  before_syntax = 6, // We still have to specify the syntax
122  after_syntax = 7 // We still have to specify the syntax
123  };
124 
125  // Enum type to help classify the type for string that has been saved.
126  // This helps in the unparsing to make sure that line feeds are introduced properly.
127  //
128  // Rama (08/17/07): Adding a CpreprocessorDeadIfDeclaration and its support
129  // in various files.
131  {
132  // This is treated as an error
134 
135  // These are a classification for comments
139 
140  // FMZ(5/14/2010): Added freeform comments (started with "!")
142 
143  // DQ (11/20/2008): Added classification for blank line (a language independent form of comment).
145 
146  // These used to be translated into IR nodes (and will be in the future).
160 
161  // DQ (10/19/2005): Added CPP warning directive
164 
165  // AS (11/18/05): Added macro support (these are generated by the Wave
166  // support, but need to be better documented as to what they mean).
169 
170  // AS & LIAO (8/12/2008): A PreprocessingInfo that is a
171  // hand made MacroCall that will expand into a valid statement.
173 
174  // DQ (11/28/2008): What does this mean!
175  // A line replacement will replace a sub-tree in the AST
176  // after a node with position (filename,line)
178 
179  // The is the 'extern "C" {' construct. Note that this is not captured in
180  // the EDG AST and it is required to be captured as part of the CPP and
181  // comment preprocessing.
184 
185  // DQ (11/17/2008): Added support for #ident
187 
188  // DQ (11/17/2008): This handles the case CPP declarations (called "linemarkers")
189  // (see Google for more details) such as: "# 1 "test2008_05.F90"", "# 1 "<built-in>"",
190  // "# 1 "<command line>"" "# 1 "test2008_05.F90""
191  // The first token is the line number,
192  // the second token is the filename (or string),
193  // the optional tokens (zero or more) are flags:
194  // '1' indicates the start of a new file.
195  // '2' indicates returning to a file (having included another file).
196  // '3' indicates that the following text comes from a system header file, so certain warnings should be supressed.
197  // '4' indicates that the following text should be treated as being wrapped in an implicit 'extern "C"' block
199 
200  // DQ (2/2/2014): permit raw text to be specified as a extremely simple way to add text to the unparsing of the AST.
201  // Note that it is the user's responcability to have the text be legal code. Additionall any language constructs
202  // added using this mechanism will ot show up in the AST. So it is not possible to reference functions (for example)
203  // added using this mechanism to build function calls as transformation elsewhere in the code. But one could add
204  // the function via AST transformations and the function body using a mechanism provided here and that would define
205  // a simple appoach to adding large complex functions for which it is impractical to build up an AST.
207 
209  };
210 
211  // DQ (7/10/2004): Make the data private
212  private:
213 
214  // DQ (4/19/2006): Use the SgFileInfo object to hold the more complete
215  // information about the filename, line number, and column number.
217  // int lineNumber;
218  // int columnNumber;
219 
220  // Use string class to improve implementation
221  // char* stringPointer;
222  std::string internalString;
223 
225 
226  // enum value representing a classification of the different types of directives
228 
229  // Corresponding enum value
231 
232  // DQ (11/28/2008): Support for CPP generated linemarkers
236 
237 // This is part of Wave support in ROSE.
238 // #ifndef USE_ROSE
239  public:
240 /*
241  // AS using the lexer_token from boost_wave in order to store structures
242  typedef boost::wave::cpplexer::lex_token<> token_type;
243  typedef std::vector<token_type> token_container;
244  typedef std::list<token_type> token_list_container;
245  typedef std::vector<std::list<token_type> > token_container_container;
246 */
247  private:
248  // FIXME: To support Jochens AST binary save work the tokenSteam must
249  // have a pointer type.
250 
251 #ifndef ROSE_SKIP_COMPILATION_OF_WAVE
252  // A stream of tokens representing the current prerpocessing info
253  // object. This is equivalent to the internal string, but of cause
254  // contains more information since it is a tokenized stream.
256 
257  public:
258  typedef struct r_include_directive
259  {
260  // The parameter 'directive' contains the (expanded) file name found after
261  // the #include directive. This has the format '<file>', '"file"' or 'file'.
263  // The paths plus name to the include directive filename
264  std::string absname;
265  std::string relname;
267 
268  // Internal representation of a macro #define directive
269  typedef struct r_macro_def
270  {
278 
279  // Internal representation of a macro call
280  // e.g #define MACRO_CALL int x;
281  // MACRO_CALL
282 #if 0
283  typedef struct r_macro_call
284  {
285  bool is_functionlike;
286  PreprocessingInfo* macro_def;
287  token_type macro_call;
288  token_container_container arguments;
289  token_container expanded_macro;
290 
291  // Get string representation of the expanded macro
292  std::string get_expanded_string()
293  {
294  std::ostringstream os;
295  token_container::const_iterator iter;
296  for (iter=expanded_macro.begin(); iter!=expanded_macro.end(); iter++)
297  os << (*iter).get_value();
298  return os.str();
299  }
300 
301  r_macro_call() : macro_call(), arguments(),expanded_macro() {}
302  } rose_macro_call;
303 #else
304  // DQ (3/9/2013): Modified to address SWIG error.
306  {
312 
313  // DQ (3/9/2013): The function definition is moved to the source file to get around SWIG error.
314  // Get string representation of the expanded macro
315  std::string get_expanded_string();
316 
317  // DQ (3/9/2013): The function definition is moved to the source file to get around SWIG error.
318  rose_macro_call();
319  };
320 #endif
321 
322  private:
323  // AS add macro definition
325  // AS add macro call
327  // AS include directive
329 
330 #endif
331 
332  // member functions
333  public:
336 
337 // #ifndef USE_ROSE
338 #ifndef ROSE_SKIP_COMPILATION_OF_WAVE
339  // AS (112105) Added constructors to support macros
345 #endif
346 
347  // This constructor is called from the C++ code generated from the lex file (preproc.lex)
348  // PreprocessingInfo(DirectiveType, const char *inputStringPointer, int line_no , int col_no,
349  // int nol, RelativePositionType relPos, bool copiedFlag, bool unparsedFlag) ROSE_DEPRECATED_FUNCTION;
350 
351  // DQ (7/19/2008): I have removed the bool copiedFlag and bool unparsedFlag parameters because they are not used
352  // and are present only because in an older implementation of the unparser it would make the PreprocessingInfo
353  // as unparsed (and maybe copied) but this sort of side-effect of the unparser was later removed to make the
354  // unparsing side-effect free.
355  // DQ (4/19/2006): Use the SgFileInfo object to hold the more complete
356  // information about the filename, line number, and column number.
357  // DQ (3/15/2006): Build constructor that uses C++ string as input (to replace the char* based constructor)
358  // PreprocessingInfo(DirectiveType, const std::string inputString, int line_no , int col_no,
359  // int nol, RelativePositionType relPos, bool copiedFlag, bool unparsedFlag);
360  // PreprocessingInfo(DirectiveType, const std::string & inputString,
361  // const std::string & filenameString, int line_no , int col_no,
362  // int nol, RelativePositionType relPos, bool copiedFlag, bool unparsedFlag );
363  PreprocessingInfo(DirectiveType, const std::string & inputString,
364  const std::string & filenameString, int line_no , int col_no,
365  int nol, RelativePositionType relPos );
366 
367  // Copy constructor
368  PreprocessingInfo(const PreprocessingInfo &prepInfo);
369 
370  void display(const std::string & label) const;
371 
372  // Access functions
373  int getLineNumber() const;
374  int getColumnNumber() const;
375  std::string getString() const;
376  void setString ( const std::string & s );
377  int getStringLength() const;
381 
382  // Number of lines occupied by this comment (count the number of line feeds)
383  int getNumberOfLines() const;
384  int getColumnNumberOfEndOfString() const; // only correct for single line directives
385 
386  // Used in unparse to string mechanism
387  // char* removeLeadingWhiteSpace (const char* inputStringPointer);
388 
389  // DQ (8/6/2006): Modified to make these static functions
390  // useful for debugging
391  static std::string directiveTypeName (const DirectiveType & directive);
392  static std::string relativePositionName (const RelativePositionType & position);
393 
394  // JH (01/03/2006) methods for packing the PreprocessingInfo data, in order to store it into
395  // a file and rebuild it!
396  unsigned int packed_size () const;
397 
398  // JH (01/032006) This pack methods might cause memory leaks. Think of deleting them after stored to file ...
399  char* packed() const;
400  void unpacked( char* storePointer );
401 
402  // DQ (4/19/2006): Added Sg_File_Info objects to each PreprocessingInfo object
403  Sg_File_Info* get_file_info() const;
404  void set_file_info( Sg_File_Info* info );
405 
406 
407  // DQ (11/28/2008): Support for CPP generated linemarkers
411 
412  // DQ (11/28/2008): Support for CPP generated linemarkers
414  void set_filenameForCompilerGeneratedLinemarker( std::string x );
416 
417 // #ifndef USE_ROSE
418 #ifndef ROSE_SKIP_COMPILATION_OF_WAVE
419  // Wave specific member functions.
420  public:
421  // Access functions to get the macro call or macro definition.
422  // These are NULL if the type is not CMacroCall or
423  // CpreprocessorDefineDeclaration
427 
431 
432 #endif
433 
434  // DQ (12/30/2013): Adding support to supress output of macros that are self-referential.
435  // e.g. "#define foo X->foo", which would be expanded a second time in the backend processing.
436  // Note that if we don't output the #define, then we still might have a problem if there was
437  // code that depended upon a "#ifdef foo". So this handling is not without some risk, but it
438  // always better to use the token stream unparsing for these cases.
439  bool isSelfReferential();
440  std::string getMacroName();
441  };
442 
443 // DQ (10/15/2002) Changed list element from "PreprocessingInfo" to
444 // "PreprocessingInfo*" to avoid redundant copying of internal data.
445 // Define a new data type for the container that stores the
446 // PreprocessingInfo objects attached to an AST node
447 typedef Rose_STL_Container<PreprocessingInfo*> AttachedPreprocessingInfoType;
448 
450  {
451  private:
452  // DQ replaced use of old list class with STL
453  std::vector<PreprocessingInfo*> attributeList;
454 
456 
457  // [DT] 3/15/2000 -- Name of file from which the directives come.
458  // char fileName[256];
459  std::string fileName;
460 
461  // 3/16/2000 -- Index into the list. Not sure if this is really
462  // necessary. See implementation in unparser.C.
463  //
464  // This is where the current line number is stored while we
465  // go off and unparse a different include file. This really should have
466  // been stored in a static structure (I think) rather than in this list.
467  int index;
468 
469  // DQ (12/15/2012): Save file ids that are from filenames referenced in #line
470  // directives, these will be considered equivalent to the input source filename.
471  std::set<int> filenameIdSet;
472 
473  public:
474  // DQ (11/19/2008): Added language selection support for handling comments
476  {
483  };
484 
487  // DQ (4/19/2006): Adding SgFileInfo objects so we need to pass in a filename string
488  // void addElement(PreprocessingInfo::DirectiveType, const char *pLine, int lineNumber, int columnNumber, int numberOfLines);
489  void addElement(PreprocessingInfo::DirectiveType, const std::string & pLine, const std::string & filename, int lineNumber, int columnNumber, int numberOfLines);
490 #if 1
491  // DQ (5/9/2007): This is required for WAVE support.
492  // DQ (4/13/2007): I would like to remove this function, but it is used by WAVE support within ROSE.
493  void addElement( PreprocessingInfo &pRef );
494 #endif
495  // void addElements( ROSEAttributesList &);
497 
498 #if 1
499  // DQ (5/9/2007): This is required for WAVE support.
500  // DQ (4/13/2007): I would like to remove this function
501  void insertElement( PreprocessingInfo & pRef );
502 #endif
503 
504  // [DT] 3/15/2000 -- Interface to fileName member.
505  void setFileName(const std::string & fName);
506  std::string getFileName();
507 
508  // 3/16/2000 -- Interface to index member.
509  void setIndex(int i);
510  int getIndex();
511 
513  int size(void);
514  int getLength(void);
515  void deepClean(void);
516  void clean(void);
517 
518  // DQ (9/19/2013): generate the number associated with each position relative to the attached IR node.
519  // size_t numberByRelativePosition(PreprocessingInfo::RelativePositionType pos);
520 
521  // Access function for list
522  std::vector<PreprocessingInfo*> & getList() { return attributeList; };
523 
524  void display ( const std::string & label ); // DQ 02/18/2001 -- For debugging.
525 
526  // DQ (1/21/2008): Added access function to save the raw token stream from the lex pass.
529 
530  // This function processes the token stream to generate the input for what weaves the
531  // CPP directives and comments into the AST. All other tokens are ignore in this pass.
532  void generatePreprocessorDirectivesAndCommentsForAST( const std::string & filename );
533 
534  // DQ (11/26/2008): This is old code!
535  // Collection comments and CPP directives for fixed format (easier case)
536  // void collectFixedFormatPreprocessorDirectivesAndCommentsForAST( const std::string & filename );
537 
538  // DQ (11/16/2008): Adding support for recognition of CPP directives outside of the lex tokenization.
539  void collectPreprocessorDirectivesAndCommentsForAST( const std::string & filename, languageTypeEnum languageType );
540 
541  // DQ (11/17/2008): Refactored the code.
542  bool isFortran77Comment( const std::string & line );
543  bool isFortran90Comment( const std::string & line );
544  bool isCppDirective( const std::string & line, PreprocessingInfo::DirectiveType & cppDeclarationKind, std::string & restOfTheLine );
545 
546  // DQ (12/15/2012): traverse the attributeList and process all of the #line directives to generate a
547  // list of file ids that should be considered equivalent to that of the input source file's filename.
548  // std::set<int> generateFileIdListFromLineDirectives();
550 
551  // DQ (12/15/2012): Added access function.
552  std::set<int> & get_filenameIdSet();
553 
554  // DQ (9/29/2013): Added to support adding processed CPP directives and comments as tokens to token list.
556  };
557 
558 //
559 // [DT] 3/16/2000 -- Want to have preprocessing info for
560 // each file included from the main source file.
561 //
563  {
564  private:
565  // DQ replaced use of old list class with STL
566  // std::vector<ROSEAttributesList*> attributeListList;
567  // std::map<std::string,ROSEAttributesList*>* attrMap;
568  std::map<std::string, ROSEAttributesList*> attributeListMap;
569 
570  public:
573  // void addList ( ROSEAttributesList* listPointer );
574  void addList ( std::string fileName, ROSEAttributesList* listPointer );
575  // void addList(ROSEAttributesList &aRef);
576  // void insertList(ROSEAttributesList &aRef);
577  // ROSEAttributesList* operator[](int i);
578  // ROSEAttributesList* findList ( const std::string & fName );
579 
580  // Check to see if the ROSEAttributesList for the fName (filename) is in the container
581  bool isInList ( const std::string & fName );
582 
583  // int size(void);
584  // int getLength(void);
585  void dumpContents(void); // [DT] 3/16/2000 -- For debugging.
586  void deepClean(void);
587  void clean(void);
588  ROSEAttributesList & operator[]( const std::string & fName);
589 
590  // Access function for list
591  // std::vector<ROSEAttributesList*> & getList() { return attributeListList; };
592  std::map<std::string, ROSEAttributesList*> & getList() { return attributeListMap; };
593  void display ( const std::string & label ); // DQ 02/18/2001 -- For debugging.
594  };
595 
596 
597 // #ifndef USE_ROSE
598 #ifndef ROSE_SKIP_COMPILATION_OF_WAVE
599 
601 
602 #endif
603 
604 #endif