Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f0530f

Browse files
committed
Improve castNode notation by introducing list-extraction-specific variants.
This extends the castNode() notation introduced by commit 5bcab11 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/[email protected]
1 parent 56dd8e8 commit 8f0530f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+176
-168
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,7 @@ JumbleRangeTable(pgssJumbleState *jstate, List *rtable)
23932393

23942394
foreach(lc, rtable)
23952395
{
2396-
RangeTblEntry *rte = castNode(RangeTblEntry, lfirst(lc));
2396+
RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc);
23972397

23982398
APP_JUMB(rte->rtekind);
23992399
switch (rte->rtekind)
@@ -2656,7 +2656,7 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
26562656
JumbleExpr(jstate, (Node *) caseexpr->arg);
26572657
foreach(temp, caseexpr->args)
26582658
{
2659-
CaseWhen *when = castNode(CaseWhen, lfirst(temp));
2659+
CaseWhen *when = lfirst_node(CaseWhen, temp);
26602660

26612661
JumbleExpr(jstate, (Node *) when->expr);
26622662
JumbleExpr(jstate, (Node *) when->result);

contrib/postgres_fdw/deparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ deparseExplicitTargetList(List *tlist, List **retrieved_attrs,
13501350

13511351
foreach(lc, tlist)
13521352
{
1353-
TargetEntry *tle = castNode(TargetEntry, lfirst(lc));
1353+
TargetEntry *tle = lfirst_node(TargetEntry, lc);
13541354

13551355
if (i > 0)
13561356
appendStringInfoString(buf, ", ");

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ postgresGetForeignPlan(PlannerInfo *root,
11691169
*/
11701170
foreach(lc, scan_clauses)
11711171
{
1172-
RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lc));
1172+
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
11731173

11741174
/* Ignore any pseudoconstants, they're dealt with elsewhere */
11751175
if (rinfo->pseudoconstant)
@@ -5022,8 +5022,8 @@ conversion_error_callback(void *arg)
50225022
EState *estate = fsstate->ss.ps.state;
50235023
TargetEntry *tle;
50245024

5025-
tle = castNode(TargetEntry, list_nth(fsplan->fdw_scan_tlist,
5026-
errpos->cur_attno - 1));
5025+
tle = list_nth_node(TargetEntry, fsplan->fdw_scan_tlist,
5026+
errpos->cur_attno - 1);
50275027

50285028
/*
50295029
* Target list can have Vars and expressions. For Vars, we can get

src/backend/catalog/objectaddress.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ get_object_address(ObjectType objtype, Node *object,
854854

855855
objlist = castNode(List, object);
856856
domaddr = get_object_address_type(OBJECT_DOMAIN,
857-
castNode(TypeName, linitial(objlist)),
857+
linitial_node(TypeName, objlist),
858858
missing_ok);
859859
constrname = strVal(lsecond(objlist));
860860

@@ -932,8 +932,8 @@ get_object_address(ObjectType objtype, Node *object,
932932
break;
933933
case OBJECT_CAST:
934934
{
935-
TypeName *sourcetype = castNode(TypeName, linitial(castNode(List, object)));
936-
TypeName *targettype = castNode(TypeName, lsecond(castNode(List, object)));
935+
TypeName *sourcetype = linitial_node(TypeName, castNode(List, object));
936+
TypeName *targettype = lsecond_node(TypeName, castNode(List, object));
937937
Oid sourcetypeid;
938938
Oid targettypeid;
939939

@@ -947,7 +947,7 @@ get_object_address(ObjectType objtype, Node *object,
947947
break;
948948
case OBJECT_TRANSFORM:
949949
{
950-
TypeName *typename = castNode(TypeName, linitial(castNode(List, object)));
950+
TypeName *typename = linitial_node(TypeName, castNode(List, object));
951951
char *langname = strVal(lsecond(castNode(List, object)));
952952
Oid type_id = LookupTypeNameOid(NULL, typename, missing_ok);
953953
Oid lang_id = get_language_oid(langname, missing_ok);
@@ -1597,7 +1597,7 @@ get_object_address_opf_member(ObjectType objtype,
15971597
{
15981598
ObjectAddress typaddr;
15991599

1600-
typenames[i] = castNode(TypeName, lfirst(cell));
1600+
typenames[i] = lfirst_node(TypeName, cell);
16011601
typaddr = get_object_address_type(OBJECT_TYPE, typenames[i], missing_ok);
16021602
typeoids[i] = typaddr.objectId;
16031603
if (++i >= 2)
@@ -2319,8 +2319,8 @@ check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address,
23192319
case OBJECT_CAST:
23202320
{
23212321
/* We can only check permissions on the source/target types */
2322-
TypeName *sourcetype = castNode(TypeName, linitial(castNode(List, object)));
2323-
TypeName *targettype = castNode(TypeName, lsecond(castNode(List, object)));
2322+
TypeName *sourcetype = linitial_node(TypeName, castNode(List, object));
2323+
TypeName *targettype = lsecond_node(TypeName, castNode(List, object));
23242324
Oid sourcetypeid = typenameTypeId(NULL, sourcetype);
23252325
Oid targettypeid = typenameTypeId(NULL, targettype);
23262326

@@ -2345,7 +2345,7 @@ check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address,
23452345
break;
23462346
case OBJECT_TRANSFORM:
23472347
{
2348-
TypeName *typename = castNode(TypeName, linitial(castNode(List, object)));
2348+
TypeName *typename = linitial_node(TypeName, castNode(List, object));
23492349
Oid typeid = typenameTypeId(NULL, typename);
23502350

23512351
if (!pg_type_ownercheck(typeid, roleid))

src/backend/catalog/pg_proc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
928928
querytree_list = NIL;
929929
foreach(lc, raw_parsetree_list)
930930
{
931-
RawStmt *parsetree = castNode(RawStmt, lfirst(lc));
931+
RawStmt *parsetree = lfirst_node(RawStmt, lc);
932932
List *querytree_sublist;
933933

934934
querytree_sublist = pg_analyze_and_rewrite_params(parsetree,

src/backend/commands/aggregatecmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ DefineAggregate(ParseState *pstate, List *name, List *args, bool oldstyle, List
109109
aggKind = AGGKIND_ORDERED_SET;
110110
else
111111
numDirectArgs = 0;
112-
args = castNode(List, linitial(args));
112+
args = linitial_node(List, args);
113113
}
114114

115115
/* Examine aggregate's definition clauses */
116116
foreach(pl, parameters)
117117
{
118-
DefElem *defel = castNode(DefElem, lfirst(pl));
118+
DefElem *defel = lfirst_node(DefElem, pl);
119119

120120
/*
121121
* sfunc1, stype1, and initcond1 are accepted as obsolete spellings

src/backend/commands/async.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ AtSubCommit_Notify(void)
16361636
List *parentPendingActions;
16371637
List *parentPendingNotifies;
16381638

1639-
parentPendingActions = castNode(List, linitial(upperPendingActions));
1639+
parentPendingActions = linitial_node(List, upperPendingActions);
16401640
upperPendingActions = list_delete_first(upperPendingActions);
16411641

16421642
Assert(list_length(upperPendingActions) ==
@@ -1647,7 +1647,7 @@ AtSubCommit_Notify(void)
16471647
*/
16481648
pendingActions = list_concat(parentPendingActions, pendingActions);
16491649

1650-
parentPendingNotifies = castNode(List, linitial(upperPendingNotifies));
1650+
parentPendingNotifies = linitial_node(List, upperPendingNotifies);
16511651
upperPendingNotifies = list_delete_first(upperPendingNotifies);
16521652

16531653
Assert(list_length(upperPendingNotifies) ==
@@ -1679,13 +1679,13 @@ AtSubAbort_Notify(void)
16791679
*/
16801680
while (list_length(upperPendingActions) > my_level - 2)
16811681
{
1682-
pendingActions = castNode(List, linitial(upperPendingActions));
1682+
pendingActions = linitial_node(List, upperPendingActions);
16831683
upperPendingActions = list_delete_first(upperPendingActions);
16841684
}
16851685

16861686
while (list_length(upperPendingNotifies) > my_level - 2)
16871687
{
1688-
pendingNotifies = castNode(List, linitial(upperPendingNotifies));
1688+
pendingNotifies = linitial_node(List, upperPendingNotifies);
16891689
upperPendingNotifies = list_delete_first(upperPendingNotifies);
16901690
}
16911691
}

src/backend/commands/collationcmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_e
7171

7272
foreach(pl, parameters)
7373
{
74-
DefElem *defel = castNode(DefElem, lfirst(pl));
74+
DefElem *defel = lfirst_node(DefElem, pl);
7575
DefElem **defelp;
7676

7777
if (pg_strcasecmp(defel->defname, "from") == 0)

src/backend/commands/copy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ ProcessCopyOptions(ParseState *pstate,
10341034
/* Extract options from the statement node tree */
10351035
foreach(option, options)
10361036
{
1037-
DefElem *defel = castNode(DefElem, lfirst(option));
1037+
DefElem *defel = lfirst_node(DefElem, option);
10381038

10391039
if (strcmp(defel->defname, "format") == 0)
10401040
{
@@ -1488,7 +1488,7 @@ BeginCopy(ParseState *pstate,
14881488
/* examine queries to determine which error message to issue */
14891489
foreach(lc, rewritten)
14901490
{
1491-
Query *q = castNode(Query, lfirst(lc));
1491+
Query *q = lfirst_node(Query, lc);
14921492

14931493
if (q->querySource == QSRC_QUAL_INSTEAD_RULE)
14941494
ereport(ERROR,
@@ -1505,7 +1505,7 @@ BeginCopy(ParseState *pstate,
15051505
errmsg("multi-statement DO INSTEAD rules are not supported for COPY")));
15061506
}
15071507

1508-
query = castNode(Query, linitial(rewritten));
1508+
query = linitial_node(Query, rewritten);
15091509

15101510
/* The grammar allows SELECT INTO, but we don't support that */
15111511
if (query->utilityStmt != NULL &&

src/backend/commands/createas.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
323323
elog(ERROR, "unexpected rewrite result for %s",
324324
is_matview ? "CREATE MATERIALIZED VIEW" :
325325
"CREATE TABLE AS SELECT");
326-
query = castNode(Query, linitial(rewritten));
326+
query = linitial_node(Query, rewritten);
327327
Assert(query->commandType == CMD_SELECT);
328328

329329
/* plan the query --- note we disallow parallelism */

0 commit comments

Comments
 (0)