1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-29 22:49:41 +03:00

Improve castNode notation by introducing list-extraction-specific variants.

This extends the castNode() notation introduced by commit 5bcab1114 to
provide, in one step, extraction of a list cell's pointer and coercion to
a concrete node type.  For example, "lfirst_node(Foo, lc)" is the same
as "castNode(Foo, lfirst(lc))".  Almost half of the uses of castNode
that have appeared so far include a list extraction call, so this is
pretty widely useful, and it saves a few more keystrokes compared to the
old way.

As with the previous patch, back-patch the addition of these macros to
pg_list.h, so that the notation will be available when back-patching.

Patch by me, after an idea of Andrew Gierth's.

Discussion: https://postgr.es/m/14197.1491841216@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2017-04-10 13:51:29 -04:00
parent 56dd8e85c4
commit 8f0530f580
65 changed files with 176 additions and 168 deletions

View File

@@ -829,7 +829,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
AttrNumber attr_num;
TargetEntry *tle;
col = castNode(ResTarget, lfirst(icols));
col = lfirst_node(ResTarget, icols);
attr_num = (AttrNumber) lfirst_int(attnos);
tle = makeTargetEntry(expr,
@@ -954,7 +954,7 @@ transformInsertRow(ParseState *pstate, List *exprlist,
Expr *expr = (Expr *) lfirst(lc);
ResTarget *col;
col = castNode(ResTarget, lfirst(icols));
col = lfirst_node(ResTarget, icols);
expr = transformAssignedExpr(pstate, expr,
EXPR_KIND_INSERT_TARGET,
@@ -2300,7 +2300,7 @@ transformUpdateTargetList(ParseState *pstate, List *origTlist)
}
if (orig_tl == NULL)
elog(ERROR, "UPDATE target count mismatch --- internal error");
origTarget = castNode(ResTarget, lfirst(orig_tl));
origTarget = lfirst_node(ResTarget, orig_tl);
attrno = attnameAttNum(pstate->p_target_relation,
origTarget->name, true);

View File

@@ -798,7 +798,7 @@ stmtmulti: stmtmulti ';' stmt
if ($1 != NIL)
{
/* update length of previous stmt */
updateRawStmtEnd(castNode(RawStmt, llast($1)), @2);
updateRawStmtEnd(llast_node(RawStmt, $1), @2);
}
if ($3 != NULL)
$$ = lappend($1, makeRawStmt($3, @2 + 1));

View File

@@ -514,7 +514,7 @@ assign_collations_walker(Node *node, assign_collations_context *context)
if (qtree->targetList == NIL)
return false;
tent = castNode(TargetEntry, linitial(qtree->targetList));
tent = linitial_node(TargetEntry, qtree->targetList);
if (tent->resjunk)
return false;
@@ -649,7 +649,7 @@ assign_collations_walker(Node *node, assign_collations_context *context)
foreach(lc, expr->args)
{
CaseWhen *when = castNode(CaseWhen, lfirst(lc));
CaseWhen *when = lfirst_node(CaseWhen, lc);
/*
* The condition expressions mustn't affect
@@ -865,7 +865,7 @@ assign_aggregate_collations(Aggref *aggref,
/* Process aggregated args, holding resjunk ones at arm's length */
foreach(lc, aggref->args)
{
TargetEntry *tle = castNode(TargetEntry, lfirst(lc));
TargetEntry *tle = lfirst_node(TargetEntry, lc);
if (tle->resjunk)
assign_expr_collations(loccontext->pstate, (Node *) tle);
@@ -909,7 +909,7 @@ assign_ordered_set_collations(Aggref *aggref,
/* Process aggregated args appropriately */
foreach(lc, aggref->args)
{
TargetEntry *tle = castNode(TargetEntry, lfirst(lc));
TargetEntry *tle = lfirst_node(TargetEntry, lc);
if (merge_sort_collations)
(void) assign_collations_walker((Node *) tle, loccontext);

View File

@@ -1669,7 +1669,7 @@ transformCaseExpr(ParseState *pstate, CaseExpr *c)
resultexprs = NIL;
foreach(l, c->args)
{
CaseWhen *w = castNode(CaseWhen, lfirst(l));
CaseWhen *w = lfirst_node(CaseWhen, l);
CaseWhen *neww = makeNode(CaseWhen);
Node *warg;
@@ -2334,7 +2334,7 @@ transformXmlExpr(ParseState *pstate, XmlExpr *x)
foreach(lc, x->named_args)
{
ResTarget *r = castNode(ResTarget, lfirst(lc));
ResTarget *r = lfirst_node(ResTarget, lc);
Node *expr;
char *argname;

View File

@@ -337,7 +337,7 @@ transformArraySubscripts(ParseState *pstate,
*/
foreach(idx, indirection)
{
A_Indices *ai = castNode(A_Indices, lfirst(idx));
A_Indices *ai = lfirst_node(A_Indices, idx);
Node *subexpr;
if (isSlice)

View File

@@ -931,7 +931,7 @@ markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte,
JoinExpr *j;
if (rtindex > 0 && rtindex <= list_length(pstate->p_joinexprs))
j = castNode(JoinExpr, list_nth(pstate->p_joinexprs, rtindex - 1));
j = list_nth_node(JoinExpr, pstate->p_joinexprs, rtindex - 1);
else
j = NULL;
if (j == NULL)

View File

@@ -478,7 +478,7 @@ TypeNameListToString(List *typenames)
initStringInfo(&string);
foreach(l, typenames)
{
TypeName *typeName = castNode(TypeName, lfirst(l));
TypeName *typeName = lfirst_node(TypeName, l);
if (l != list_head(typenames))
appendStringInfoChar(&string, ',');
@@ -719,7 +719,7 @@ typeStringToTypeName(const char *str)
*/
if (list_length(raw_parsetree_list) != 1)
goto fail;
stmt = (SelectStmt *) castNode(RawStmt, linitial(raw_parsetree_list))->stmt;
stmt = (SelectStmt *) linitial_node(RawStmt, raw_parsetree_list)->stmt;
if (stmt == NULL ||
!IsA(stmt, SelectStmt) ||
stmt->distinctClause != NIL ||

View File

@@ -388,7 +388,7 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
foreach(option, seqoptions)
{
DefElem *defel = castNode(DefElem, lfirst(option));
DefElem *defel = lfirst_node(DefElem, option);
if (strcmp(defel->defname, "sequence_name") == 0)
{
@@ -605,7 +605,7 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
foreach(clist, column->constraints)
{
Constraint *constraint = castNode(Constraint, lfirst(clist));
Constraint *constraint = lfirst_node(Constraint, clist);
switch (constraint->contype)
{
@@ -1635,7 +1635,7 @@ transformIndexConstraints(CreateStmtContext *cxt)
*/
foreach(lc, cxt->ixconstraints)
{
Constraint *constraint = castNode(Constraint, lfirst(lc));
Constraint *constraint = lfirst_node(Constraint, lc);
Assert(constraint->contype == CONSTR_PRIMARY ||
constraint->contype == CONSTR_UNIQUE ||
@@ -1956,8 +1956,8 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
List *opname;
Assert(list_length(pair) == 2);
elem = castNode(IndexElem, linitial(pair));
opname = castNode(List, lsecond(pair));
elem = linitial_node(IndexElem, pair);
opname = lsecond_node(List, pair);
index->indexParams = lappend(index->indexParams, elem);
index->excludeOpNames = lappend(index->excludeOpNames, opname);
@@ -1984,7 +1984,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
foreach(columns, cxt->columns)
{
column = castNode(ColumnDef, lfirst(columns));
column = lfirst_node(ColumnDef, columns);
if (strcmp(column->colname, key) == 0)
{
found = true;
@@ -2013,7 +2013,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
foreach(inher, cxt->inhRelations)
{
RangeVar *inh = castNode(RangeVar, lfirst(inher));
RangeVar *inh = lfirst_node(RangeVar, inher);
Relation rel;
int count;
@@ -2823,7 +2823,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
*/
foreach(lc, castNode(List, cmd->def))
{
DefElem *def = castNode(DefElem, lfirst(lc));
DefElem *def = lfirst_node(DefElem, lc);
if (strcmp(def->defname, "generated") == 0)
newdef = lappend(newdef, def);
@@ -2900,7 +2900,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
*/
foreach(l, cxt.alist)
{
IndexStmt *idxstmt = castNode(IndexStmt, lfirst(l));
IndexStmt *idxstmt = lfirst_node(IndexStmt, l);
idxstmt = transformIndexStmt(relid, idxstmt, queryString);
newcmd = makeNode(AlterTableCmd);