66
77use super :: names:: * ;
88use super :: * ;
9- use crate :: ast;
109use crate :: ast:: * ;
1110use crate :: data:: * ;
1211use crate :: named_entity:: { Signature , * } ;
12+ use crate :: { ast, named_entity, HasTokenSpan } ;
1313use analyze:: * ;
1414use fnv:: FnvHashMap ;
1515use std:: collections:: hash_map:: Entry ;
1616
17+ impl Declaration {
18+ pub fn is_allowed_in_context ( & self , parent : & AnyEntKind ) -> bool {
19+ use Declaration :: * ;
20+ use ObjectClass :: * ;
21+ match parent {
22+ AnyEntKind :: Design ( Design :: Architecture ( ..) )
23+ | AnyEntKind :: Concurrent ( Some ( Concurrent :: Block | Concurrent :: Generate ) ) => matches ! (
24+ self ,
25+ Object ( ObjectDeclaration {
26+ class: Constant | Signal | SharedVariable ,
27+ ..
28+ } ) | File ( _)
29+ | Type ( _)
30+ | Component ( _)
31+ | Attribute ( _)
32+ | Alias ( _)
33+ | SubprogramDeclaration ( _)
34+ | SubprogramInstantiation ( _)
35+ | SubprogramBody ( _)
36+ | Use ( _)
37+ | Package ( _)
38+ | Configuration ( _)
39+ ) ,
40+ AnyEntKind :: Design ( Design :: Configuration ) => {
41+ matches ! ( self , Use ( _) | Attribute ( ast:: Attribute :: Specification ( _) ) )
42+ }
43+ AnyEntKind :: Design ( Design :: Entity ( ..) ) => matches ! (
44+ self ,
45+ Object ( _)
46+ | File ( _)
47+ | Type ( _)
48+ | Attribute ( _)
49+ | Alias ( _)
50+ | SubprogramDeclaration ( _)
51+ | SubprogramInstantiation ( _)
52+ | SubprogramBody ( _)
53+ | Use ( _)
54+ | Package ( _)
55+ ) ,
56+ AnyEntKind :: Design ( Design :: PackageBody | Design :: UninstPackage ( ..) )
57+ | AnyEntKind :: Overloaded ( Overloaded :: SubprogramDecl ( _) | Overloaded :: Subprogram ( _) )
58+ | AnyEntKind :: Concurrent ( Some ( Concurrent :: Process ) )
59+ | AnyEntKind :: Type ( named_entity:: Type :: Protected ( ..) ) => matches ! (
60+ self ,
61+ Object ( ObjectDeclaration {
62+ class: Constant | Variable | SharedVariable ,
63+ ..
64+ } ) | File ( _)
65+ | Type ( _)
66+ | Attribute ( _)
67+ | Alias ( _)
68+ | SubprogramDeclaration ( _)
69+ | SubprogramInstantiation ( _)
70+ | SubprogramBody ( _)
71+ | Use ( _)
72+ | Package ( _)
73+ ) ,
74+ AnyEntKind :: Design ( Design :: Package ( ..) ) => matches ! (
75+ self ,
76+ Object ( _)
77+ | File ( _)
78+ | Type ( _)
79+ | Component ( _)
80+ | Attribute ( _)
81+ | Alias ( _)
82+ | SubprogramDeclaration ( _)
83+ | SubprogramInstantiation ( _)
84+ | Use ( _)
85+ | Package ( _)
86+ ) ,
87+ _ => {
88+ // AnyEntKind::Library is used in tests for a generic declarative region
89+ if !( cfg ! ( test) && matches ! ( parent, AnyEntKind :: Library ) ) {
90+ debug_assert ! ( false , "Parent should be a declarative region" ) ;
91+ }
92+ true
93+ }
94+ }
95+ }
96+ }
97+
1798impl < ' a > AnalyzeContext < ' a > {
1899 pub fn analyze_declarative_part (
19100 & self ,
@@ -29,6 +110,13 @@ impl<'a> AnalyzeContext<'a> {
29110
30111 let ( decl, remaining) = declarations[ i..] . split_first_mut ( ) . unwrap ( ) ;
31112
113+ if !decl. is_allowed_in_context ( parent. kind ( ) ) {
114+ diagnostics. error (
115+ decl. get_pos ( self . ctx ) ,
116+ format ! ( "{} declaration not allowed here" , decl. describe( ) , ) ,
117+ )
118+ }
119+
32120 match decl {
33121 Declaration :: Type ( type_decl) => match type_decl. def {
34122 TypeDefinition :: Incomplete ( ref mut reference) => {
0 commit comments