From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id 1B3D33857C52 for ; Fri, 17 Jul 2020 03:22:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1B3D33857C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=2ndquadrant.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=craig.ringer@2ndquadrant.com Received: by mail-lj1-x22c.google.com with SMTP id f5so10971546ljj.10 for ; Thu, 16 Jul 2020 20:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=6kbR5SFICG4sJujK/ewyi0otaXSSJNu7rqLJ746Kyzg=; b=Q+QB3+0YqEVyqAnZ5P9MAbQLNcurd/9gwxJdPeSkD/AWR38+5I/s0L72w0vVRg1cTH xpg2buuoXbSo1shnxFmyBxaqCIu/mHbAKMqK+nUwKVAeWZEI5zsxgjN9awoxEtF5ARug Rf7YDFJBW8JEaOp6C0OXGYSZSobGUXkQE8z0i9g3vlnELC3rcpeB/7HBhpRvBhb73ZFT npmuuL9IaBkgx44guJ/hHvNbwbYz78KYkXZHFqjKM3NVy4RpcpTXqk7uLNtlTJ4ozbZa pDMomvdQp3COZMd2V0f+T6MHd2+lskTbABKaojgKQP7wE413lpmmOHw6Tw5+SlogmrIW oVRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=6kbR5SFICG4sJujK/ewyi0otaXSSJNu7rqLJ746Kyzg=; b=YzJQhwwE2E/qLej+T6P8smpj+oHLwuP3E8jVW+H4CI87fejlrKpFqUtzuT5Cv1BP8D hNTDSUDZQ+CqJNDfXooMTBmt6FrReP6fQgOBGGQGJbMNMYqej4nozJuJUZOavxfecU7g P/5bEVFSUAg5qdMW8fcSfMM+tD/Fukx0JnaBK/a09SzlkNGbLlnRhsNhepS3hVTvlIyx l7galtveLnrN7kiA0SOfCQcFLyq7dH0PY6ZY0KaTHw+bXYOHTy3yVbVWGLg1kwrl61od mOMT2atrsoZsoKPfMHCTQrA//frnSiJ2xmuV/Ey7MxpsyOAT68P6/RYgZcUF31VMigsj XZYQ== X-Gm-Message-State: AOAM532By+GR4Q+h/EzNr58aaufBcUr843+wifaKjvCU6rt8WSwHER1U 22jTeWZLgoTnWE5JUenKspBaf9QQ62S+K8sPpZUury7zZAHn9Q== X-Google-Smtp-Source: ABdhPJyPooneBSPWsP0T6R6qfHekOTwB/BpEzsGvd5jtSY2+hsmeV/1SnScQfWc2XBZzJ2fEBNsbmRwEmpfb54Q9OZY= X-Received: by 2002:a2e:544c:: with SMTP id y12mr3325181ljd.132.1594956163411; Thu, 16 Jul 2020 20:22:43 -0700 (PDT) MIME-Version: 1.0 From: Craig Ringer Date: Fri, 17 Jul 2020 11:22:32 +0800 Message-ID: Subject: Safe-dereference operator? To: systemtap@sourceware.org X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: systemtap@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Systemtap mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jul 2020 03:22:46 -0000 Hi TL;DR: Proposal to add a safe-dereference pseudo-operator like Groovy's ?. to the language to simplify chasing pointer chains. ?. is basically an abbreviated ternary without the multiple-evaluation hazard: a ?. b ?. c is (assuming that all expressions are pure and side-effect free) the same as: (!a ? 0 : (!a.b ? 0 : a.b.c)) i.e. this operator behaves like -> if the left operand is non-null. If the left operand is null, it returns 0 and short-circuits out the right hand expression. This is extremely useful when traversing chains of possibly-null pointers. Systemtap would possibly spell it as ?>, ?->, or similar, since the systemtap convention is to use "->" for all dereferences. Rationale: When writing systemtap code I routinely find myself writing logic to dereference a chain of pointers and return the result, unless any pointer on the chain is null, in which case a default is returned. Say I want to return user_string($var->foo->bar) I land up writing ret = ""; foo = $var if (foo != 0) { bar = @cast(foo, "Foo")->bar; if (bar != 0) { ret = user_string(bar); } } return ret This is a tad tedious. Especially since systemtap does not track typeinfo for locals, so you have to explicitly type everything. It's possible to instead try { return user_string($var->foo->bar) } catch (ex) { return ""; } but I'm not sure what performance implication that has, and more importantly it may mask other unexpected errors that should not be caught and swallowed. A macro implementation is possible, but as noted above, prone to multiple-eval hazards. So - does this seem like something worth having in the language? If so, would anyone here be willing to offer some advice on how feasible it might be to implement, and where in the code I might want to start looking? -- Craig Ringer http://www.2ndQuadrant.com/ 2ndQuadrant - PostgreSQL Solutions for the Enterprise