eRPC Generator (erpcgen)  Rev. 1.7.0
NXP Semiconductors
SymbolScanner.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _EMBEDDED_RPC__SYMBOLSCANNER_H_
37 #define _EMBEDDED_RPC__SYMBOLSCANNER_H_
38 
39 #include "AstWalker.h"
40 #include "types/AliasType.h"
41 #include "types/EnumMember.h"
42 #include "types/EnumType.h"
43 #include "types/Interface.h"
44 #include "types/Program.h"
45 #include "types/StructType.h"
46 #include "types/SymbolScope.h"
47 #include "types/UnionType.h"
48 
50 // Classes
52 
53 namespace erpcgen {
54 
58 class SymbolScanner : public AstWalker
59 {
60 public:
69  : m_globals(globals)
70  , m_currentInterface(nullptr)
71  , m_currentStruct(nullptr)
72  , m_currentProgram(nullptr)
73  , m_currentEnum(nullptr)
74  , m_currentAlias(nullptr)
75  , m_currentUnion(nullptr)
76  {
77  }
78 
87  SymbolScanner(SymbolScope *globals, std::string fileName)
88  : AstWalker(fileName)
89  , m_globals(globals)
90  , m_currentInterface(nullptr)
91  , m_currentStruct(nullptr)
92  , m_currentProgram(nullptr)
93  , m_currentEnum(nullptr)
94  , m_currentAlias(nullptr)
95  , m_currentUnion(nullptr)
96  {
97  }
98 
102  virtual ~SymbolScanner() {}
103 
110 
111 protected:
120  std::vector<UnionCase *> m_emptyUnionCases;
121  std::map<std::string, DataType *> m_forwardDeclarations;
130  virtual void handleRoot(AstNode *node, bottom_up);
131 
141  virtual AstNode *handleProgram(AstNode *node, top_down);
142 
152  virtual AstNode *handleProgram(AstNode *node, bottom_up);
153 
163  virtual AstNode *handleConst(AstNode *node, bottom_up);
164 
177  virtual AstNode *handleType(AstNode *node, top_down);
178 
190  virtual AstNode *handleType(AstNode *node, bottom_up);
191 
203  virtual AstNode *handleEnum(AstNode *node, top_down);
204 
216  virtual AstNode *handleEnum(AstNode *node, bottom_up);
217 
229  virtual AstNode *handleEnumMember(AstNode *node, bottom_up);
230 
238  bool enumMemberHasValue(AstNode *enumMember);
239 
249  virtual AstNode *handleExpr(AstNode *node, bottom_up);
250 
268  virtual AstNode *handleBinaryOp(AstNode *node, bottom_up);
269 
287  virtual AstNode *handleUnaryOp(AstNode *node, bottom_up);
288 
303  virtual AstNode *handleStruct(AstNode *node, top_down);
304 
316  virtual AstNode *handleStruct(AstNode *node, bottom_up);
317 
330  virtual AstNode *handleStructMember(AstNode *node, bottom_up);
331 
332  // TODO: Update doxygen for union functions
344  virtual AstNode *handleUnion(AstNode *node, top_down);
345 
357  virtual AstNode *handleUnion(AstNode *node, bottom_up);
358 
368  virtual AstNode *handleUnionCase(AstNode *node, top_down);
369 
381  virtual AstNode *handleUnionCase(AstNode *node, bottom_up);
382 
397  virtual AstNode *handleInterface(AstNode *node, top_down);
398 
410  virtual AstNode *handleInterface(AstNode *node, bottom_up);
411 
424  virtual AstNode *handleFunction(AstNode *node, top_down);
425 
437  virtual AstNode *handleFunction(AstNode *node, bottom_up);
438 
453  virtual AstNode *handleParam(AstNode *node, top_down);
454 
466  virtual AstNode *handleParam(AstNode *node, bottom_up);
467 
475  void setParameterDirection(StructMember *param, AstNode *directionNode);
476 
488 
498 
507  bool containsStructEnumDeclaration(const AstNode *typeNode);
508 
525  DataType *lookupDataType(const AstNode *typeNode);
526 
540  DataType *createListType(const AstNode *typeNode);
541 
554  DataType *createArrayType(const AstNode *typeNode);
555 
573  DataType *lookupDataTypeByName(const Token &tok, SymbolScope *scope, bool recursive = true);
574 
587  uint64_t getIntExprValue(const AstNode *exprNode);
588 
597  bool rhsIsAStringLiteral(AstNode *rhs);
598 
607  bool rhsIsAnExpression(AstNode *rhs);
608 
617  Value *getValueForConst(AstNode *const node, DataType *const constDataType);
618 
625  void addAnnotations(AstNode *childTok, Symbol *symbol);
626 
633  void checkAnnotationBeforeAdding(AstNode *annotation, Symbol *symbol);
634 
642  Value *getAnnotationValue(AstNode *annotationNode);
643 
651  Annotation::program_lang_t getAnnotationLang(AstNode *annotationNode);
652 
660 
670  void addDoxygenComments(Symbol *symbol, AstNode *above, AstNode *trailing);
671 
683  StructMember *createCallbackParam(StructMember *structMember, const std::string &name);
684 
690  void addForwardDeclaration(DataType *dataType);
691 
697  void removeForwardDeclaration(DataType *dataType);
698 
704  void addGlobalSymbol(Symbol *symbol);
705 };
706 
707 } // namespace erpcgen
708 
709 #endif // _EMBEDDED_RPC__SYMBOLSCANNER_H_
virtual AstNode * handleStructMember(AstNode *node, bottom_up)
This function end handle struct member.
Definition: SymbolScanner.cpp:751
void addDoxygenComments(Symbol *symbol, AstNode *above, AstNode *trailing)
This function sets to given symbol given doxygen comments.
Definition: SymbolScanner.cpp:1707
virtual ~SymbolScanner()
Destructor.
Definition: SymbolScanner.h:102
Annotation::program_lang_t getAnnotationLang(AstNode *annotationNode)
Helper function to get programming language type if specified for which is annotation intended...
Definition: SymbolScanner.cpp:1560
DataType * lookupDataTypeByName(const Token &tok, SymbolScope *scope, bool recursive=true)
This function returns pointer to new type object.
Definition: SymbolScanner.cpp:1335
virtual AstNode * handleEnumMember(AstNode *node, bottom_up)
This function end handle enum member.
Definition: SymbolScanner.cpp:274
void checkAnnotationBeforeAdding(AstNode *annotation, Symbol *symbol)
This function check annotation just before it will be added to symbol.
Definition: SymbolScanner.cpp:1583
bool containsStructEnumDeclaration(const AstNode *typeNode)
Returns true when given Astnode is enum or structure declaration.
Definition: SymbolScanner.cpp:1359
DataType * lookupDataType(const AstNode *typeNode)
This function returns pointer to new type object.
Definition: SymbolScanner.cpp:1365
Scans for symbol names.
Definition: SymbolScanner.h:58
bool rhsIsAStringLiteral(AstNode *rhs)
Determines if the right hand side of a constant declaration is a string literal.
Definition: SymbolScanner.cpp:152
Member of a struct.
Definition: StructMember.h:63
uint64_t getIntExprValue(const AstNode *exprNode)
This function returns token integer value.
Definition: SymbolScanner.cpp:1454
Interface * m_currentInterface
Definition: SymbolScanner.h:114
virtual AstNode * handleStruct(AstNode *node, top_down)
This function start handle struct.
Definition: SymbolScanner.cpp:650
StructType * m_currentStruct
Definition: SymbolScanner.h:115
virtual AstNode * handleUnaryOp(AstNode *node, bottom_up)
This function end handle unary operator.
Definition: SymbolScanner.cpp:555
virtual AstNode * handleConst(AstNode *node, bottom_up)
This function handles a constant definition.
Definition: SymbolScanner.cpp:75
Typedef.
Definition: AliasType.h:51
Homogeneous AST node class.
Definition: AstNode.h:61
virtual AstNode * handleParam(AstNode *node, top_down)
This function start handle function parameter.
Definition: SymbolScanner.cpp:1198
DataType * getDataTypeForConst(AstNode *typeNode)
This function returns the data type for a constant variable.
Definition: SymbolScanner.cpp:157
Performs a depth-first walk of an AST.
Definition: AstWalker.h:67
Program declaration.
Definition: Program.h:53
Structure data type.
Definition: StructType.h:54
Base class for all named declarations in the IDL.
Definition: Symbol.h:53
Value * getAnnotationValue(AstNode *annotationNode)
Helper function to get Value from annotation AstNode.
Definition: SymbolScanner.cpp:1544
virtual AstNode * handleEnum(AstNode *node, top_down)
This function end handle enum.
Definition: SymbolScanner.cpp:207
void addGlobalSymbol(Symbol *symbol)
This function add symbol into global symbol scope.
Definition: SymbolScanner.cpp:1792
SymbolScanner(SymbolScope *globals, std::string fileName)
This function is constructor of symbol scanner class.
Definition: SymbolScanner.h:87
virtual AstNode * handleProgram(AstNode *node, top_down)
This function start handle program.
Definition: SymbolScanner.cpp:624
bool enumMemberHasValue(AstNode *enumMember)
Check to see if enum member is assigned a value by the user.
Definition: SymbolScanner.cpp:312
virtual AstNode * handleExpr(AstNode *node, bottom_up)
This function start handle expression.
Definition: SymbolScanner.cpp:1329
Value * getValueForConst(AstNode *const node, DataType *const constDataType)
Gets the value object for the const variable.
Definition: SymbolScanner.cpp:89
Abstract base class for values of arbitrary types.
Definition: Value.h:47
EnumType * m_currentEnum
Definition: SymbolScanner.h:117
Program * m_currentProgram
Definition: SymbolScanner.h:116
virtual AstNode * handleInterface(AstNode *node, top_down)
This function start handle interface.
Definition: SymbolScanner.cpp:1008
virtual AstNode * handleFunction(AstNode *node, top_down)
This function start handle function.
Definition: SymbolScanner.cpp:1037
Program * getProgram()
Program is out of global symbol scope.
Definition: SymbolScanner.h:109
A scoped namespace containing symbols.
Definition: SymbolScope.h:53
StructMember * createCallbackParam(StructMember *structMember, const std::string &name)
This function creates new function parameter.
Definition: SymbolScanner.cpp:1722
bool rhsIsAnExpression(AstNode *rhs)
Determines if the right hand side of a constant declaration is an expression.
Definition: SymbolScanner.cpp:148
void addAnnotations(AstNode *childTok, Symbol *symbol)
This function add annotations to vector of symbol annotations.
Definition: SymbolScanner.cpp:1497
Base class for data types.
Definition: DataType.h:51
SymbolScope * m_globals
Definition: SymbolScanner.h:113
Encapsulates all information about a token.
Definition: Token.h:86
DataType * createArrayType(const AstNode *typeNode)
This function returns pointer to new array object.
Definition: SymbolScanner.cpp:1403
Value * getValueFromSymbol(Token &tok)
Get value from symbol table for name defined by given token.
Definition: SymbolScanner.cpp:1418
Specifier type to select bottom-up handler using ADL.
Definition: AstWalker.h:107
virtual AstNode * handleUnionCase(AstNode *node, top_down)
Definition: SymbolScanner.cpp:885
Enumerate data type.
Definition: EnumType.h:53
virtual AstNode * handleBinaryOp(AstNode *node, bottom_up)
This function end handle binary operator.
Definition: SymbolScanner.cpp:332
void setParameterDirection(StructMember *param, AstNode *directionNode)
set parameter direction: in, out, inout
Definition: SymbolScanner.cpp:1298
virtual void handleRoot(AstNode *node, bottom_up)
This function is called at the end of scanning.
Definition: SymbolScanner.cpp:56
std::map< std::string, DataType * > m_forwardDeclarations
Definition: SymbolScanner.h:121
DataType * createListType(const AstNode *typeNode)
This function returns pointer to new list object.
Definition: SymbolScanner.cpp:1391
virtual AstNode * handleType(AstNode *node, top_down)
This function start handle type.
Definition: SymbolScanner.cpp:170
Discriminated union data type.
Definition: UnionType.h:53
void scanStructForAnnotations()
Controlling annotations used on structure members.
Definition: SymbolScanner.cpp:1633
An interface that contains functions.
Definition: Interface.h:54
SymbolScanner(SymbolScope *globals)
This function is constructor of symbol scanner class.
Definition: SymbolScanner.h:68
virtual AstNode * handleUnion(AstNode *node, top_down)
Handles Union data type while traversing down the AST.
Definition: SymbolScanner.cpp:787
std::vector< UnionCase * > m_emptyUnionCases
Definition: SymbolScanner.h:120
void removeForwardDeclaration(DataType *dataType)
This function unregister union/structure declarations.
Definition: SymbolScanner.cpp:1776
UnionType * m_currentUnion
Definition: SymbolScanner.h:119
Definition: AstNode.h:51
AliasType * m_currentAlias
Definition: SymbolScanner.h:118
Specifier type to select top-down handler using ADL.
Definition: AstWalker.h:104
void addForwardDeclaration(DataType *dataType)
This function registers forward union/structure declarations.
Definition: SymbolScanner.cpp:1748