| 820 | } |
| 821 | |
| 822 | AstNode *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, ""); |
nothing calls this directly
no test coverage detected