@@ -12,7 +12,7 @@ type SupertypeMap = HashMap<String, BTreeSet<String>>;
12
12
///
13
13
/// `language` - the language for which we're generating a library
14
14
/// `classes` - the list of classes to write.
15
- pub fn write ( language : & Language , classes : & [ ql:: Class ] ) -> std:: io:: Result < ( ) > {
15
+ pub fn write ( language : & Language , classes : & [ ql:: TopLevel ] ) -> std:: io:: Result < ( ) > {
16
16
println ! (
17
17
"Writing QL library for {} to '{}'" ,
18
18
& language. name,
@@ -130,89 +130,14 @@ fn create_none_predicate(
130
130
}
131
131
}
132
132
133
- /// Creates the special `Location` class to wrap the location table.
134
- fn create_location_class ( ) -> ql:: Class {
135
- let to_string = ql:: Predicate {
136
- name : "toString" . to_owned ( ) ,
137
- overridden : false ,
138
- return_type : Some ( ql:: Type :: String ) ,
139
- formal_parameters : vec ! [ ] ,
140
- body : ql:: Expression :: Equals (
141
- Box :: new ( ql:: Expression :: Var ( "result" . to_owned ( ) ) ) ,
142
- Box :: new ( ql:: Expression :: String ( "Location" . to_owned ( ) ) ) ,
143
- ) ,
144
- } ;
145
- let has_location_info = ql:: Predicate {
146
- name : "hasLocationInfo" . to_owned ( ) ,
147
- overridden : false ,
148
- return_type : None ,
149
- formal_parameters : vec ! [
150
- ql:: FormalParameter {
151
- name: "filePath" . to_owned( ) ,
152
- param_type: ql:: Type :: String ,
153
- } ,
154
- ql:: FormalParameter {
155
- name: "startLine" . to_owned( ) ,
156
- param_type: ql:: Type :: Int ,
157
- } ,
158
- ql:: FormalParameter {
159
- name: "startColumn" . to_owned( ) ,
160
- param_type: ql:: Type :: Int ,
161
- } ,
162
- ql:: FormalParameter {
163
- name: "endLine" . to_owned( ) ,
164
- param_type: ql:: Type :: Int ,
165
- } ,
166
- ql:: FormalParameter {
167
- name: "endColumn" . to_owned( ) ,
168
- param_type: ql:: Type :: Int ,
169
- } ,
170
- ] ,
171
- body : ql:: Expression :: Exists (
172
- vec ! [ ql:: FormalParameter {
173
- param_type: ql:: Type :: Normal ( "File" . to_owned( ) ) ,
174
- name: "f" . to_owned( ) ,
175
- } ] ,
176
- Box :: new ( ql:: Expression :: And ( vec ! [
177
- ql:: Expression :: Pred (
178
- "locations_default" . to_owned( ) ,
179
- vec![
180
- ql:: Expression :: Var ( "this" . to_owned( ) ) ,
181
- ql:: Expression :: Var ( "f" . to_owned( ) ) ,
182
- ql:: Expression :: Var ( "startLine" . to_owned( ) ) ,
183
- ql:: Expression :: Var ( "startColumn" . to_owned( ) ) ,
184
- ql:: Expression :: Var ( "endLine" . to_owned( ) ) ,
185
- ql:: Expression :: Var ( "endColumn" . to_owned( ) ) ,
186
- ] ,
187
- ) ,
188
- ql:: Expression :: Equals (
189
- Box :: new( ql:: Expression :: Var ( "filePath" . to_owned( ) ) ) ,
190
- Box :: new( ql:: Expression :: Dot (
191
- Box :: new( ql:: Expression :: Var ( "f" . to_owned( ) ) ) ,
192
- "getAbsolutePath" . to_owned( ) ,
193
- vec![ ] ,
194
- ) ) ,
195
- ) ,
196
- ] ) ) ,
197
- ) ,
198
- } ;
199
- ql:: Class {
200
- name : "Location" . to_owned ( ) ,
201
- supertypes : vec ! [ ql:: Type :: AtType ( "location" . to_owned( ) ) ] ,
202
- is_abstract : false ,
203
- characteristic_predicate : None ,
204
- predicates : vec ! [ to_string, has_location_info] ,
205
- }
206
- }
207
-
208
133
/// Given the name of the parent node, and its field information, returns the
209
134
/// name of the field's type. This may be an ad-hoc union of all the possible
210
135
/// types the field can take, in which case we create a new class and push it to
211
136
/// `classes`.
212
137
fn create_field_class (
213
138
parent_name : & str ,
214
139
field : & node_types:: Field ,
215
- classes : & mut Vec < ql:: Class > ,
140
+ classes : & mut Vec < ql:: TopLevel > ,
216
141
supertype_map : & SupertypeMap ,
217
142
) -> String {
218
143
if field. types . len ( ) == 1 {
@@ -225,7 +150,7 @@ fn create_field_class(
225
150
let field_union_name = format ! ( "{}_{}_type" , parent_name, & field. get_name( ) ) ;
226
151
let field_union_name = node_types:: escape_name ( & field_union_name) ;
227
152
let class_name = dbscheme_name_to_class_name ( & field_union_name) ;
228
- classes. push ( ql:: Class {
153
+ classes. push ( ql:: TopLevel :: Class ( ql :: Class {
229
154
name : class_name. clone ( ) ,
230
155
is_abstract : false ,
231
156
supertypes : [
@@ -235,7 +160,7 @@ fn create_field_class(
235
160
. concat ( ) ,
236
161
characteristic_predicate : None ,
237
162
predicates : vec ! [ ] ,
238
- } ) ;
163
+ } ) ) ;
239
164
field_union_name
240
165
}
241
166
}
@@ -455,9 +380,13 @@ fn create_field_getters(
455
380
}
456
381
457
382
/// Converts the given node types into CodeQL classes wrapping the dbscheme.
458
- pub fn convert_nodes ( nodes : & Vec < node_types:: Entry > ) -> Vec < ql:: Class > {
383
+ pub fn convert_nodes ( nodes : & Vec < node_types:: Entry > ) -> Vec < ql:: TopLevel > {
459
384
let supertype_map = create_supertype_map ( nodes) ;
460
- let mut classes: Vec < ql:: Class > = vec ! [ create_location_class( ) , create_top_class( ) ] ;
385
+ let mut classes: Vec < ql:: TopLevel > = vec ! [
386
+ ql:: TopLevel :: Import ( "codeql.files.FileSystem" . to_owned( ) ) ,
387
+ ql:: TopLevel :: Import ( "codeql.Locations" . to_owned( ) ) ,
388
+ ql:: TopLevel :: Class ( create_top_class( ) ) ,
389
+ ] ;
461
390
462
391
for node in nodes {
463
392
match & node {
@@ -472,7 +401,7 @@ pub fn convert_nodes(nodes: &Vec<node_types::Entry>) -> Vec<ql::Class> {
472
401
type_name. named ,
473
402
) ) ;
474
403
let class_name = dbscheme_name_to_class_name ( & union_name) ;
475
- classes. push ( ql:: Class {
404
+ classes. push ( ql:: TopLevel :: Class ( ql :: Class {
476
405
name : class_name. clone ( ) ,
477
406
is_abstract : false ,
478
407
supertypes : [
@@ -482,7 +411,7 @@ pub fn convert_nodes(nodes: &Vec<node_types::Entry>) -> Vec<ql::Class> {
482
411
. concat ( ) ,
483
412
characteristic_predicate : None ,
484
413
predicates : vec ! [ ] ,
485
- } ) ;
414
+ } ) ) ;
486
415
}
487
416
node_types:: Entry :: Table { type_name, fields } => {
488
417
// Count how many columns there will be in the main table.
@@ -556,7 +485,7 @@ pub fn convert_nodes(nodes: &Vec<node_types::Entry>) -> Vec<ql::Class> {
556
485
} ) ;
557
486
}
558
487
559
- classes. push ( main_class) ;
488
+ classes. push ( ql :: TopLevel :: Class ( main_class) ) ;
560
489
}
561
490
}
562
491
}
0 commit comments