From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 0190A3858D1E for ; Fri, 30 Dec 2022 17:30:08 +0000 (GMT) Received: by mail-pl1-x62c.google.com with SMTP id 17so22545907pll.0 for ; Fri, 30 Dec 2022 09:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=ZDBu4hW+MbP193OE5fyhfXWUl406tI6QBkuVFFEmjXE=; b=W8ZkZuo++dpoIxEd5eOhCgcwhmHj5PcQdnBgk/RxYR0p9wjgmUHj4gdFmT+HmtqIeo pPbGeHMrQLFbskz2j6uy6u43XFNybRe+tfai0mXCaCJ7sCO5DNs1JdQagrCOX9YvYyDI nOzKTE4VVQyW1J/64EeJuCJn0zVNA0WcMc8HY6w4JmGow1jK0iLm3M8QrDcFGWpQAQXw sIvl2q/BaOEHE5J94Vgm1YgOLnKOKalbdnfi13x9YQLHrUnry+VEESnLhM0DwlyNj0IH xCuJibVDq4UBZHhX349/HB5+r8MU1Mrzm0fHdI7All69/dtmphBsEy7jcaiaitDafwHo X2MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZDBu4hW+MbP193OE5fyhfXWUl406tI6QBkuVFFEmjXE=; b=v5nB3o2YLCV3pZRSu2TmrLU+RU6ObVEBArD6o2oHaffEv0P6lC+f2qKBdaJkOkSQHf fCTu6E4IdThBxy1yDOPjE+qlh4xcSe9I7RpfxSR2fswpzlDULH7jJ5ufvO3ZsU+0gh9Q JYsFeCaF56QNMytvUIl72pOJu8F3hNSCibmfupDzo3IS57CKYzGjqAg1COgTPZ9YuLyw NUCe9pirGEThBAfGo+PBp3uEy+n9UM21hHxM/n/KcN2nAPOJeSY5hQR2S1BwiebPe+pa lyqsrohX0K/VaYy4oQf9rZCT9GW47k6ewicZDtSYT2vQ5kLhFf6VVMdd78y4Uk5FsP65 v8lQ== X-Gm-Message-State: AFqh2kp+6wBkAEINIUHh1JP4jClJfp25TBr89ul2XJFLK5VFZ85Cfqm3 PXoMLCF6ZOV49WRTeNs+hwBjeBZAwyS4P/aiCQ4a8QUnk2s= X-Google-Smtp-Source: AMrXdXuPiQlKPz7VRGoL4i2///f3q0FQ7NydG0r4jXowdLCU6Z2cf4QpU/e8wW5qnSh9WfbQ1NY4UQ8+Ah24Rjlaz1U= X-Received: by 2002:a17:902:ca93:b0:18f:438a:cfe3 with SMTP id v19-20020a170902ca9300b0018f438acfe3mr2291841pld.124.1672421406578; Fri, 30 Dec 2022 09:30:06 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Gavin Ray Date: Fri, 30 Dec 2022 12:29:54 -0500 Message-ID: Subject: Re: Difficulty in implementing a GCC plugin for [[invariant]] contract functions in classes/structs without prior experience? To: gcc@gcc.gnu.org Content-Type: multipart/alternative; boundary="000000000000da9cdd05f10ef38c" X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000da9cdd05f10ef38c Content-Type: text/plain; charset="UTF-8" Hey all, I've continued to work on this and have it mostly finished The implementation is here GCC [[invariant]] plugin for Design by Contract (WIP) (github.com) I have posted the final remaining question to StackOverflow in case anyone knows It's about how to insert a call to a C++ Member Function, from another Mem-Fn using the GIMPLE API: GCC GIMPLE C++ API, how to insert a call to a member-function from another member-function? - Stack Overflow Would be grateful for any advice. I asked Iain Buclaw, who maintains GDC, and he was able to help but doesn't have a ton of expertise in the C++ frontend. Thanks all On Thu, Dec 29, 2022 at 1:33 PM Gavin Ray wrote: > So I have prototyped the code here, there are two places where I am > confused: > GCC [[invariant]] plugin for Design by Contract (WIP) (github.com) > > > 1. Is it proper to cast like this? > > tree args = NULL_TREE; > tree tmp_tree = NULL_TREE; > vec** args_ptr = (vec**)&args; > > tree call = build_new_method_call(class_type, id, args_ptr, fun->decl, > LOOKUP_NORMAL, &tmp_tree, tf_warning_or_error); > > 2. At the bottom, I want to call "invariant()" before "return", but I'm > not sure how to phrase this in code. The following doesn't work quite right: > because you can't compare "gsi_stmt()" and NULL_TREE > > // If the last block is a return block, insert the call before the > return statement. > if (gsi_stmt(gsi) != NULL_TREE && gimple_code(gsi_stmt(gsi)) == > GIMPLE_RETURN) > gsi_insert_before(&gsi, g, GSI_SAME_STMT); > else > gsi_insert_after(&gsi, g, GSI_SAME_STMT); > > > > > On Thu, Dec 29, 2022 at 10:37 AM Gavin Ray wrote: > >> Hey all, >> >> The feature I appreciate most about the D programming language is its >> "Design by Contract" feature. >> Contract programming - Dlang Tour >> >> >> With the recent merge of Contracts to GCC master, C++ comes close to >> this, with support for function-based contracts. >> The most powerful contract though, is the "invariant" contract: >> >> *invariant() is a special member function of struct and class types that >>> allows sanity checking an object's state during its whole lifetime:* >> >> >>> >>> *- invariant() is called after the constructor has run and before the >>> destructor is called.* >>> *- invariant() is called before entering a member function* >>> *- invariant() is called after exiting a member function.**- Class >>> invariants are inherited. That means that a derived class invariant will >>> implicitly call the base class invariant.* >> >> >> I'm very interested in prototyping a GCC plugin to implement support for >> transforming all member function calls in class/struct types >> such that a call to the [[invariant]]-annotated function (if any) is >> placed at the beginning and end of method bodies. >> >> Does anyone have any idea whether something like this would be suitable >> for a first plugin, >> or if there would be roadblocks/technical challenges to implementing such >> a thing? >> >> I'd be grateful for any advice. >> >> Thank you, >> Gavin =) >> >> --000000000000da9cdd05f10ef38c--