MCPcopy Create free account
hub / github.com/EmbeddedRPC/erpc / getValueForConst

Method getValueForConst

erpcgen/src/SymbolScanner.cpp:66–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66Value *SymbolScanner::getValueForConst(AstNode *const node, DataType *const constDataType)
67{
68 Value *constVal = nullptr;
69 bool isStringConst = constDataType->getTrueDataType()->isString();
70 if (rhsIsAStringLiteral(node))
71 {
72 if (!isStringConst)
73 {
74 throw syntax_error2("Attempt to assign a string to a non-string data type",
75 node->getChild(0)->getToken().getLocation(), m_fileName);
76 }
77 else
78 {
79 constVal = node->getChild(2)->getTokenValue();
80 }
81 }
82 else if (rhsIsAnExpression(node))
83 {
84 // const node -> virtual expression node -> actual expression (id or number)
85 Token &rhsExpressionChildTok = node->getChild(2)->getChild(0)->getToken();
86 if (rhsExpressionChildTok.isNumberTok())
87 {
88 if (isStringConst)
89 {
90 throw syntax_error2("Attempt to assign a number to a string data type",
91 node->getChild(0)->getToken().getLocation(), m_fileName);
92 }
93 else
94 {
95 constVal = rhsExpressionChildTok.getValue();
96 }
97 }
98 else if (rhsExpressionChildTok.isIdentifierTok())
99 {
100 constVal = getValueFromSymbol(rhsExpressionChildTok);
101 if (kStringValue == constVal->getType())
102 {
103 if (!isStringConst)
104 {
105 delete constVal;
106 throw syntax_error2("Attempt to assign a string to a non-string data type",
107 node->getChild(0)->getToken().getLocation(), m_fileName);
108 }
109 }
110 else // kStringValue != constVal->getType()
111 {
112 if (isStringConst)
113 {
114 delete constVal;
115 throw syntax_error2("Attempt to assign a number to a string data type",
116 node->getChild(0)->getToken().getLocation(), m_fileName);
117 }
118 }
119 } // else do nothing
120 }
121 assert(nullptr != constVal);
122 return constVal;
123}

Callers

nothing calls this directly

Calls 10

syntax_error2Class · 0.85
getTrueDataTypeMethod · 0.80
getTokenMethod · 0.80
getChildMethod · 0.80
getTokenValueMethod · 0.80
isNumberTokMethod · 0.80
isIdentifierTokMethod · 0.80
getTypeMethod · 0.80
isStringMethod · 0.45
getValueMethod · 0.45

Tested by

no test coverage detected