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

Method handleUnion

erpcgen/src/SymbolScanner.cpp:822–886  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

820}
821
822AstNode *SymbolScanner::handleUnion(AstNode *node, top_down)
823{
824 Token *tok;
825 AstNode *astUnionName = node->getChild(0);
826 AstNode *astUnionDiscriminator = node->getChild(1);
827
828 /* get union type name*/
829 if (!astUnionName)
830 {
831 /* Get the name of the union variable so we can manipulate it and add it to the AST
832 under the union token */
833 string unionVariableName = node->getParent()->getChild(0)->getToken().getStringValue();
834
835 /* Create a new node in the AST for the union's name, and assign it */
836 node->appendChild(new AstNode(Token(TOK_IDENT, new StringValue(unionVariableName + "_union"))));
837
838 /* union token. */
839 tok = &node->getChild(3)->getToken();
840 }
841 else
842 {
843 /* union token for non-encapsulated discriminated unions. */
844 tok = &astUnionName->getToken();
845 };
846
847 Log::debug("union: %s\n", tok->getStringValue().c_str());
848
849 UnionType *newUnion;
850 /* get union type object */
851 if (astUnionDiscriminator)
852 {
853 /* discriminated unions. */
854 const string &discriminatorName = astUnionDiscriminator->getToken().getStringValue();
855 newUnion = new UnionType(*tok, discriminatorName);
856 }
857 else
858 {
859 /* match forward declaration with definition */
860 auto forwardDecl = m_forwardDeclarations.find(tok->getStringValue());
861 if (forwardDecl != m_forwardDeclarations.end())
862 {
863 newUnion = dynamic_cast<UnionType *>(forwardDecl->second);
864 if (newUnion != nullptr)
865 {
866 new (newUnion) UnionType(*tok, "");
867 }
868 else
869 {
870 throw syntax_error(
871 format_string("line %d: Union definition type name didn't match data type of "
872 "forward declaration from line %d.",
873 tok->getFirstLine(), forwardDecl->second->getFirstLine()));
874 }
875 }
876 else
877 {
878 /* non-encapsulated discriminated unions. */
879 newUnion = new UnionType(*tok, "");

Callers

nothing calls this directly

Calls 13

syntax_errorClass · 0.85
format_stringFunction · 0.85
getChildMethod · 0.80
getTokenMethod · 0.80
appendChildMethod · 0.80
childCountMethod · 0.80
addSymbolMethod · 0.80
setParentStructMethod · 0.80
TokenClass · 0.70
getParentMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected