1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // HandleAttributes.cc -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Fri Jul 27 10:15:06 2018 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Fri Jul 27 10:16:43 2018 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include "HandleAttributes.h" |
---|
17 | |
---|
18 | #include "CompilationState.h" |
---|
19 | #include "Common/Eval.h" |
---|
20 | #include "Common/PassVisitor.h" |
---|
21 | #include "Common/SemanticError.h" |
---|
22 | #include "ResolvExpr/Resolver.h" |
---|
23 | #include "SynTree/Attribute.h" |
---|
24 | #include "SynTree/Declaration.h" |
---|
25 | #include "SynTree/Type.h" |
---|
26 | |
---|
27 | namespace Validate { |
---|
28 | namespace { |
---|
29 | struct HandleAttributes : public WithIndexer { |
---|
30 | void previsit( ObjectDecl * decl ); |
---|
31 | void previsit( FunctionDecl * decl ); |
---|
32 | }; |
---|
33 | } // namespace |
---|
34 | |
---|
35 | void handleAttributes( std::list< Declaration * > &translationUnit ) { |
---|
36 | PassVisitor<HandleAttributes> handler; |
---|
37 | acceptAll( translationUnit, handler ); |
---|
38 | } |
---|
39 | |
---|
40 | namespace { |
---|
41 | void HandleAttributes::previsit( ObjectDecl * decl ) { |
---|
42 | for ( Attribute * attr : decl->attributes ) { |
---|
43 | std::string name = attr->normalizedName(); |
---|
44 | if (name == "init_priority") { |
---|
45 | // TODO: implement C++-like init_priority attribute |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void HandleAttributes::previsit( FunctionDecl * decl ) { |
---|
51 | for ( Attribute * attr : decl->attributes ) { |
---|
52 | std::string name = attr->normalizedName(); |
---|
53 | if (name == "constructor" || name == "destructor") { |
---|
54 | if (attr->parameters.size() == 1) { |
---|
55 | Expression *& arg = attr->parameters.front(); |
---|
56 | ResolvExpr::findSingleExpression( arg, new BasicType( Type::Qualifiers(), BasicType::LongLongSignedInt ), indexer ); |
---|
57 | auto result = eval(arg); |
---|
58 | if (! result.second) { |
---|
59 | SemanticWarning(attr->location, Warning::GccAttributes, |
---|
60 | toCString( name, " priorities must be integers from 0 to 65535 inclusive: ", arg ) ); |
---|
61 | return; |
---|
62 | } |
---|
63 | auto priority = result.first; |
---|
64 | if (priority < 101) { |
---|
65 | SemanticWarning(attr->location, Warning::GccAttributes, |
---|
66 | toCString( name, " priorities from 0 to 100 are reserved for the implementation" ) ); |
---|
67 | } else if (priority < 201 && ! buildingLibrary()) { |
---|
68 | SemanticWarning(attr->location, Warning::GccAttributes, |
---|
69 | toCString( name, " priorities from 101 to 200 are reserved for the implementation" ) ); |
---|
70 | } |
---|
71 | } else if (attr->parameters.size() > 1) { |
---|
72 | SemanticWarning(attr->location, Warning::GccAttributes, toCString( "too many arguments to ", name, " attribute" ) ); |
---|
73 | } else { |
---|
74 | SemanticWarning(attr->location, Warning::GccAttributes, toCString( "too few arguments to ", name, " attribute" ) ); |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } // namespace |
---|
80 | } // namespace Validate |
---|
81 | |
---|
82 | // Local Variables: // |
---|
83 | // tab-width: 4 // |
---|
84 | // mode: c++ // |
---|
85 | // compile-command: "make install" // |
---|
86 | // End: // |
---|