8000 centralize cloning functionality for AstNodes (#10430) · arangodb/arangodb@237a3fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 237a3fc

Browse files
authored
centralize cloning functionality for AstNodes (#10430)
1 parent 330c4a6 commit 237a3fc

File tree

2 files changed

+33
-67
lines changed

2 files changed

+33
-67
lines changed

arangod/Aql/Ast.cpp

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,23 +2383,11 @@ bool Ast::getReferencedAttributes(AstNode const* node, Variable const* variable,
23832383
return state.isSafeForOptimization;
23842384
}
23852385

2386-
/// @brief recursively clone a node
2387-
AstNode* Ast::clone(AstNode const* node) {
2386+
/// @brief copies node payload from node into copy. this is *not* copying
2387+
/// the subnodes
2388+
void Ast::copyPayload(AstNode const* node, AstNode* copy) const {
23882389
AstNodeType const type = node->type;
2389-
if (type == NODE_TYPE_NOP) {
2390-
// nop node is a singleton
2391-
return const_cast<AstNode*>(node);
2392-
}
23932390

2394-
AstNode* copy = createNode(type);
2395-
TRI_ASSERT(copy != nullptr);
2396-
2397-
// copy flags
2398-
copy->flags = node->flags;
2399-
TEMPORARILY_UNLOCK_NODE(copy); // if locked, unlock to copy properly
2400-
2401-
// special handling for certain node types
2402-
// copy payload...
24032391
if (type == NODE_TYPE_COLLECTION || type == NODE_TYPE_VIEW || type == NODE_TYPE_PARAMETER ||
24042392
type == NODE_TYPE_PARAMETER_DATASOURCE || type == NODE_TYPE_ATTRIBUTE_ACCESS ||
24052393
type == NODE_TYPE_OBJECT_ELEMENT || type == NODE_TYPE_FCALL_USER) {
@@ -2448,7 +2436,27 @@ AstNode* Ast::clone(AstNode const* node) {
24482436
break;
24492437
}
24502438
}
2439+
}
2440+
2441+
/// @brief recursively clone a node
2442+
AstNode* Ast::clone(AstNode const* node) {
2443+
AstNodeType const type = node->type;
2444+
if (type == NODE_TYPE_NOP) {
2445+
// nop node is a singleton
2446+
return const_cast<AstNode*>(node);
2447+
}
2448+
2449+
AstNode* copy = createNode(type);
2450+
TRI_ASSERT(copy != nullptr);
24512451

2452+
// copy flags
2453+
copy->flags = node->flags;
2454+
TEMPORARILY_UNLOCK_NODE(copy); // if locked, unlock to copy properly
2455+
2456+
// special handling for certain node types
2457+
// copy payload...
2458+
copyPayload(node, copy);
2459+
24522460
// recursively clone subnodes
24532461
size_t const n = node->numMembers();
24542462
copy->members.reserve(n);
@@ -2474,59 +2482,13 @@ AstNode* Ast::shallowCopyForModify(AstNode const* node) {
24742482

24752483
// special handling for certain node types
24762484
// copy payload...
2477-
if (type == NODE_TYPE_COLLECTION || type == NODE_TYPE_VIEW || type == NODE_TYPE_PARAMETER ||
2478-
type == NODE_TYPE_PARAMETER_DATASOURCE || type == NODE_TYPE_ATTRIBUTE_ACCESS ||
2479-
type == NODE_TYPE_OBJECT_ELEMENT || type == NODE_TYPE_FCALL_USER) {
2480-
copy->setStringValue(node->getStringValue(), node->getStringLength());
2481-
} else if (type == NODE_TYPE_VARIABLE || type == NODE_TYPE_REFERENCE || type == NODE_TYPE_FCALL) {
2482-
copy->setData(node->getData());
2483-
} else if (type == NODE_TYPE_UPSERT || type == NODE_TYPE_EXPANSION) {
2484-
copy->setIntValue(node->getIntValue(true));
2485-
} else if (type == NODE_TYPE_QUANTIFIER) {
2486-
copy->setIntValue(node->getIntValue(true));
2487-
} else if (type == NODE_TYPE_OPERATOR_BINARY_IN || type == NODE_TYPE_OPERATOR_BINARY_NIN ||
2488-
type == NODE_TYPE_OPERATOR_BINARY_ARRAY_IN ||
2489-
type == NODE_TYPE_OPERATOR_BINARY_ARRAY_NIN) {
2490-
// copy sortedness information
2491-
copy->setBoolValue(node->getBoolValue());
2492-
} else if (type == NODE_TYPE_ARRAY) {
2493-
if (node->isSorted()) {
2494-
copy->setFlag(DETERMINED_SORTED, VALUE_SORTED);
2495-
} else {
2496-
copy->setFlag(DETERMINED_SORTED);
2497-
}
2498-
} else if (type == NODE_TYPE_VALUE) {
2499-
switch (node->value.type) {
2500-
case VALUE_TYPE_NULL:
2501-
copy->value.type = VALUE_TYPE_NULL;
2502-
break;
2503-
case VALUE_TYPE_BOOL:
2504-
copy->value.type = VALUE_TYPE_BOOL;
2505-
copy->setBoolValue(node->getBoolValue());
2506-
break;
2507-
case VALUE_TYPE_INT:
2508-
copy->value.type = VALUE_TYPE_INT;
2509-
copy->setIntValue(node->getIntValue());
2510-
break;
2511-
case VALUE_TYPE_DOUBLE:
2512-
copy->value.type = VALUE_TYPE_DOUBLE;
2513-
copy->setDoubleValue(node->getDoubleValue());
2514-
break;
2515-
case VALUE_TYPE_STRING:
2516-
copy->value.type = VALUE_TYPE_STRING;
2517-
copy->setStringValue(node->getStringValue(), node->getStringLength());
2518-
break;
2519-
}
2520-
}
2521-
2522-
// recursively clone subnodes
2485+
copyPayload(node, copy);
2486+
2487+
// recursively add subnodes
25232488
size_t const n = node->numMembers();
2524-
if (n > 0) {
2525-
copy->members.reserve(n);
2526-
2527-
for (size_t i = 0; i < n; ++i) {
2528-
copy->addMember(node->getMemberUnchecked(i));
2529-
}
2489+
copy->members.reserve(n);
2490+
for (size_t i = 0; i < n; ++i) {
2491+
copy->addMember(node->getMemberUnchecked(i));
25302492
}
25312493

25322494
return copy;

arangod/Aql/Ast.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ class Ast {
523523

524524
void extractCollectionsFromGraph(AstNode const* graphNode);
525525

526+
/// @brief copies node payload from node into copy. this is *not* copying
527+
/// the subnodes
528+
void copyPayload(AstNode const* node, AstNode* copy) const;
529+
526530
public:
527531
/// @brief negated comparison operators
528532
static std::unordered_map<int, AstNodeType> const NegatedOperators;

0 commit comments

Comments
 (0)
0