From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by sourceware.org (Postfix) with ESMTPS id 527213858CDA for ; Thu, 29 Dec 2022 18:34:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 527213858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x102c.google.com with SMTP id p4so20015713pjk.2 for ; Thu, 29 Dec 2022 10:34:02 -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=gGvqnlgYHaIXJZEEHRH1mg/akEs6D8dGoJfz7xdxDYs=; b=Y9e/jdF0oo0RDkKwJbnV3eN1AK4ZE0jActzD/UwsncCZ+aUtPAJcWDjdbpqsr07WJN uu83PxEyUAEPji6iVfPIG3hWk6oCUR1JZe5AJDW/fq4qcZEdZrBS9Zy5Rc/3uft4+WHN 1gxgHy0iaYXlGy5HK/SnCDB0qM0KkoAiDenue5iDgItFn2EQK8rXV9NfX7UFJw5N5wKM dHb4iQt08qdFTFs1rLPco+HctwPmbDUeIheudOG1H6PDPmLTi2Uw2uGTxiS31R9PycBy FUGEYxhk67uOx1XpEHUOrSuH1Bzk27rpL4RCrEBqaMY9SX8AYjw0KnE52TezL4AVtOQC XRUQ== 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=gGvqnlgYHaIXJZEEHRH1mg/akEs6D8dGoJfz7xdxDYs=; b=Lxl9pgxPGRJ4LIvA8iuLMM8CDnO+KIo5/oSYZUZqB2Ieou+9X+KldfNtmwLJMy1gN3 jFQz9gCIC3WmmcD0gav+4OQ9TU/hV6w/6ZJDb7tOjCfH1mQv6rGDbs0AeGeH1t46BpAJ 94A0M2dzxWcuFvf70eUO7iXxDCpbhG9DWvRWxAbNp9nvjyPks1WrajOP4FlDKqzNCWjx IcBMEFoUCLsZIiLFXDbNvFI9JwVEFvx998QrZrzkzsP3TXRtyhu7f1f4yhrBGOuEeYFh 7if6bSkOqeRE9Gyd45WeQRnElARledLIWrHsg+a9AO88fSiAYmE0tuLlnhW4jKBWfB9T qUsA== X-Gm-Message-State: AFqh2kpTJTdCXRFJgj3fdGj/srqLe3RJGSPBHYzfMIEXVZDAoHNlGJKo Ay4k5d9XgR/BSh+F0vwbGBB8DUF4q3Pf7f+27hj1i1ipEgQ= X-Google-Smtp-Source: AMrXdXthbdf7pyvRLpVJ2E2YZAV4C9vPmpMHXhIv1egZTSWwtraRwpb2EB5VlzdKBCCXW1WczY+mn2LYcKgHKyuYvy0= X-Received: by 2002:a17:90a:f315:b0:225:b164:8886 with SMTP id ca21-20020a17090af31500b00225b1648886mr1990185pjb.65.1672338840600; Thu, 29 Dec 2022 10:34:00 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Gavin Ray Date: Thu, 29 Dec 2022 13:33:49 -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="00000000000089c62e05f0fbba42" X-Spam-Status: No, score=-0.0 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: --00000000000089c62e05f0fbba42 Content-Type: text/plain; charset="UTF-8" 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 =) > > --00000000000089c62e05f0fbba42--