From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id E8AAD3882659 for ; Tue, 19 Dec 2023 14:31:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E8AAD3882659 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E8AAD3882659 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::336 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702996270; cv=none; b=cF2s+1Vlt+S0PxTQAjz7UnS8MtbDD+3s4S742cnRTlBzlkTXInmIwzEgwY5rzxazFgs7sEedLhOBx94x4BsEqhC9P53CeFSVLq5QlTJx43N7yL/l1JNcS7QxxHyL/u2f4MDipH6+0a9YvZhAKicAUj5M7MKD6t5PHP6rbLRjO04= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702996270; c=relaxed/simple; bh=X0oDnRUVKvOkgdyR9+qc9KZDauqtozXxYp0yfC9nnJ4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WQVO4gHkOtgYePazktwlfuWV1759q95cRZJaZdRRDdfWYimDFbDMIhwr2MTCcYBepvWbhsEgZjM4SNTpvDsKHTztofyyXcWkB6LNIXUBkcuZwzmpAVsr0NAN9dwmEddjCwTRdnTZB8rfTO7cEmNxJifzk54tFizPVB7DUuxE8WU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-40c339d2b88so49747275e9.3 for ; Tue, 19 Dec 2023 06:31:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702996268; x=1703601068; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZKK24RzLW1cWShcphjd8K1YkPFuBQ7OtJSniuxGccQI=; b=Aih9gjrJ+W0d/T/9SEGKB9J9GtsLSFD0bHcVyuaurAjzNjWaL7S1mywPlrOfV4FMsQ b4TsaCZJR4z1k96oHFhgtXeYhnTB+M/FvsYHKaq5paoeA9NOl2VSFdUbHUJLdPG2vhnP fTE5ZRqKsbFgLY4NNcYbOBRyEzZXf7b1mslg9t2JXWM0Hgb+eV0ny7mH4XAYSg4Y/dpm TjrrBqg9GqmEbU6AUeCQYJ4qhbKGgJiSOM2/1UYBYMqt1p4w/8Yslht/D8opgYiWeHm8 PMUC8i6cEXhbkE2ncKFxuYjl8bJM5BAr1ph7bNiH3dtz7r9RIThpTsWhpi+k5UZubi1W nVMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702996268; x=1703601068; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZKK24RzLW1cWShcphjd8K1YkPFuBQ7OtJSniuxGccQI=; b=eiLxpMpdW4z4bgT32mPbx1GlnZQyx4WJfGIevgYAID27QSigaNtpPy38G23YTr2J8K rMzS+3I8TW/ZXHKRZfOATLa6vPh2DLovi9wdNLpbZ2iIng5SPf7LgedguY8FIg84LAV4 lLo9TN3GrdNeE8mZmoNtdw4SBfB4e1li/8ICpTS82hxpV3ZyHQj18g8KdQCDtFqU1DYs x/HQTLGp5AKi1X6K4AgkK4oMY+gNcTLvGd69KbwBxpjcpdaoBDqgoTT6MIBcTLHzKEiM og0NZ4IKF2IoF/R5kt4iHviVELDX3nfnXsgZWlNo48JWZuhYMpp8U7+q6Ib84qE+qxmS q8jw== X-Gm-Message-State: AOJu0YyPS0wCf669ArUVA+9r82lQBk5bHh8Enpg7JIf3GiH6rgiOENNV wRjVf0kQKA4PUTgtJ0wllADkTwiM/2tt01PAldo= X-Google-Smtp-Source: AGHT+IGKTp1zTgU7TvHQhHLX3reKGQXkeYXYvKxC7KQER+8sTP1xeY1b3eyBj3MxruOk6ur2zEt7ig== X-Received: by 2002:a05:600c:218e:b0:40c:23b9:616 with SMTP id e14-20020a05600c218e00b0040c23b90616mr9268886wme.169.1702996267799; Tue, 19 Dec 2023 06:31:07 -0800 (PST) Received: from poulhies-Precision-5550.lan ([2001:861:3382:1a90:fe1e:443:c34f:edaa]) by smtp.gmail.com with ESMTPSA id w20-20020a05600c475400b0040b4fca8620sm3066107wmo.37.2023.12.19.06.31.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 06:31:07 -0800 (PST) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Viljar Indus Subject: [COMMITTED] ada: Check all interfaces for valid iterator type Date: Tue, 19 Dec 2023 15:31:06 +0100 Message-ID: <20231219143106.456073-1-poulhies@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE 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: From: Viljar Indus gcc/ada/ * sem_ch13.adb (Valid_Default_Iterator): Check all interfaces for valid iterator type. Also improve error reporting. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/sem_ch13.adb | 103 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 13 deletions(-) diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb index 8f6fa3af0f8..6513afa0b1c 100644 --- a/gcc/ada/sem_ch13.adb +++ b/gcc/ada/sem_ch13.adb @@ -5876,39 +5876,116 @@ package body Sem_Ch13 is ------------------------------ procedure Check_Iterator_Functions is - function Valid_Default_Iterator (Subp : Entity_Id) return Boolean; - -- Check one possible interpretation for validity + function Valid_Default_Iterator (Subp : Entity_Id; + Ref_Node : Node_Id := Empty) + return Boolean; + -- Check one possible interpretation for validity. If + -- Ref_Node is present report errors on violations. ---------------------------- -- Valid_Default_Iterator -- ---------------------------- - function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is - Root_T : constant Entity_Id := Root_Type (Etype (Etype (Subp))); - Formal : Entity_Id; + function Valid_Default_Iterator (Subp : Entity_Id; + Ref_Node : Node_Id := Empty) + return Boolean + is + Return_Type : constant Entity_Id := Etype (Etype (Subp)); + Return_Node : Node_Id; + Root_T : constant Entity_Id := Root_Type (Return_Type); + Formal : Entity_Id; + + function Valid_Iterator_Name (E : Entity_Id) return Boolean + is (Chars (E) in Name_Forward_Iterator | Name_Reversible_Iterator); + + function Valid_Iterator_Name (L : Elist_Id) return Boolean; + + ------------------------- + -- Valid_Iterator_Name -- + ------------------------- + + function Valid_Iterator_Name (L : Elist_Id) return Boolean + is + Iface_Elmt : Elmt_Id := First_Elmt (L); + begin + while Present (Iface_Elmt) loop + if Valid_Iterator_Name (Node (Iface_Elmt)) then + return True; + end if; + Next_Elmt (Iface_Elmt); + end loop; + + return False; + end Valid_Iterator_Name; begin + if Subp = Any_Id then + if Present (Ref_Node) then + + -- Subp is not resolved and an error will be posted about + -- it later + + Error_Msg_N ("improper function for default iterator!", + Ref_Node); + end if; + + return False; + end if; + if not Check_Primitive_Function (Subp) then + if Present (Ref_Node) then + Error_Msg_N ("improper function for default iterator!", + Ref_Node); + Error_Msg_Sloc := Sloc (Subp); + Error_Msg_NE + ("\\default iterator defined # " + & "must be a primitive function", + Ref_Node, Subp); + end if; + return False; + end if; -- The return type must be derived from a type in an instance -- of Iterator.Interfaces, and thus its root type must have a -- predefined name. - elsif Chars (Root_T) /= Name_Forward_Iterator - and then Chars (Root_T) /= Name_Reversible_Iterator + if not Valid_Iterator_Name (Root_T) + and then not (Has_Interfaces (Return_Type) and then + Valid_Iterator_Name (Interfaces (Return_Type))) then - return False; + if Present (Ref_Node) then - else - Formal := First_Formal (Subp); + Return_Node := Result_Definition (Parent (Subp)); + + Error_Msg_N ("improper function for default iterator!", + Ref_Node); + Error_Msg_Sloc := Sloc (Return_Node); + Error_Msg_NE ("\\return type & # " + & "must inherit from either " + & "Forward_Iterator or Reversible_Iterator", + Ref_Node, Return_Node); + end if; + + return False; end if; + Formal := First_Formal (Subp); + -- False if any subsequent formal has no default expression Next_Formal (Formal); while Present (Formal) loop if No (Expression (Parent (Formal))) then + if Present (Ref_Node) then + Error_Msg_N ("improper function for default iterator!", + Ref_Node); + Error_Msg_Sloc := Sloc (Formal); + Error_Msg_NE ("\\formal parameter & # " + & "must have a default expression", + Ref_Node, Formal); + end if; + return False; end if; @@ -5920,6 +5997,8 @@ package body Sem_Ch13 is return True; end Valid_Default_Iterator; + Ignore : Boolean; + -- Start of processing for Check_Iterator_Functions begin @@ -5940,9 +6019,7 @@ package body Sem_Ch13 is -- Flag the default_iterator as well as the denoted function. - if not Valid_Default_Iterator (Entity (Expr)) then - Error_Msg_N ("improper function for default iterator!", Expr); - end if; + Ignore := Valid_Default_Iterator (Entity (Expr), Expr); else declare -- 2.43.0