Modules |
Files |
Inheritance Tree |
Inheritance Graph |
Name Index |
Config
File: Synopsis/Parser/C++/syn/ast.hh
1| // Synopsis C++ Parser: ast.hh header file
2| // Defines the AST classes in the AST namespace
3|
4| // $Id: ast.hh,v 1.22 2003/01/27 06:53:36 chalky Exp $
5| //
6| // This file is a part of Synopsis.
7| // Copyright (C) 2002 Stephen Davies
8| //
9| // Synopsis is free software; you can redistribute it and/or modify it
10| // under the terms of the GNU General Public License as published by
11| // the Free Software Foundation; either version 2 of the License, or
12| // (at your option) any later version.
13| //
14| // This program is distributed in the hope that it will be useful,
15| // but WITHOUT ANY WARRANTY; without even the implied warranty of
16| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17| // General Public License for more details.
18| //
19| // You should have received a copy of the GNU General Public License
20| // along with this program; if not, write to the Free Software
21| // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22| // 02111-1307, USA.
23|
24| // vim: set ts=8 sts=2 sw=2 et:
25| // File: ast.h
26| // A C++ class hierarchy that more or less mirrors the AST hierarchy in
27| // Python/Core.AST.
28|
29| #ifndef H_SYNOPSIS_CPP_AST
30| #define H_SYNOPSIS_CPP_AST
31|
32| #include "common.hh"
33|
34|
35| class Dictionary;
36|
37|
38| namespace Types
39| {
40| class Type;
41| class Declared;
42| class Template;
43| }
44|
45|
46| namespace AST
47| {
48|
49| class Visitor;
50|
51|
52| class SourceFile;
53|
54|
55| enum Access
56| {
57| Default = 0,
58| Public,
59| Protected,
60| Private
61| };
62|
63|
64|
65| struct Reference
66| {
67| std::string file;
68| int line;
69| ScopedName scope;
70| std::string context;
71|
72|
73| Reference()
74| : line(-1)
75| { }
76| Reference(const std::string& _file, int _line, const ScopedName& _scope, const std::string& _context)
77| : file(_file), line(_line), scope(_scope), context(_context)
78| { }
79| Reference(const Reference& other)
80| : file(other.file), line(other.line), scope(other.scope), context(other.context)
81| { }
82|
83| Reference& operator=(const Reference& other)
84| {
85| file = other.file;
86| line = other.line;
87| scope = other.scope;
88| context = other.context;
89| return *this;
90| }
91| };
92|
93|
94|
95|
96|
97|
98|
99| class Comment : public cleanup
100| {
101| public:
102|
103| typedef std::vector<Comment*> vector;
104|
105|
106| Comment(SourceFile* file, int line, const std::string& text, bool suspect=false);
107|
108|
109|
110|
111|
112|
113| SourceFile* file() const
114| {
115| return m_file;
116| }
117|
118|
119| int line() const
120| {
121| return m_line;
122| }
123|
124|
125| const std::string& text() const
126| {
127| return m_text;
128| }
129|
130|
131| void set_suspect(bool suspect)
132| {
133| m_suspect = suspect;
134| }
135|
136|
137| bool is_suspect() const
138| {
139| return m_suspect;
140| }
141|
142| private:
143|
144| SourceFile* m_file;
145|
146| int m_line;
147|
148| std::string m_text;
149|
150|
151| bool m_suspect;
152| };
153|
154|
155|
156|
157|
158|
159|
160|
161| class Declaration : public cleanup
162| {
163| public:
164|
165| typedef std::vector<Declaration*> vector;
166|
167|
168| Declaration(SourceFile* file, int line, const std::string& type, const ScopedName& name);
169|
170|
171| virtual
172| ~Declaration();
173|
174|
175| virtual void
176| accept(Visitor*);
177|
178|
179|
180|
181|
182|
183| ScopedName& name()
184| {
185| return m_name;
186| }
187|
188|
189| const ScopedName& name() const
190| {
191| return m_name;
192| }
193|
194|
195| SourceFile* file() const
196| {
197| return m_file;
198| }
199|
200|
201| void set_file(SourceFile* file)
202| {
203| m_file = file;
204| }
205|
206|
207| int line() const
208| {
209| return m_line;
210| }
211|
212|
213| const std::string& type() const
214| {
215| return m_type;
216| }
217|
218|
219|
220| void set_type(const std::string& type)
221| {
222| m_type = type;
223| }
224|
225|
226| Access access() const
227| {
228| return m_access;
229| }
230|
231|
232| void set_access(Access axs)
233| {
234| m_access = axs;
235| }
236|
237|
238| const Comment::vector& comments() const
239| {
240| return m_comments;
241| }
242|
243|
244|
245|
246| Comment::vector& comments()
247| {
248| return m_comments;
249| }
250|
251|
252|
253|
254| Types::Declared* declared();
255|
256|
257|
258|
259| const Types::Declared* declared() const;
260|
261| private:
262|
263| SourceFile* m_file;
264|
265| int m_line;
266|
267| std::string m_type;
268|
269| ScopedName m_name;
270|
271| Comment::vector m_comments;
272|
273| Access m_access;
274|
275| mutable Types::Declared* m_declared;
276| };
277|
278|
279|
280|
281|
282|
283|
284|
285|
286|
287|
288| class Include : public cleanup
289| {
290| public:
291|
292| typedef std::vector<Include*> vector;
293|
294|
295| Include(SourceFile* target, bool is_macro, bool is_next);
296|
297|
298| SourceFile* target() const
299| {
300| return m_target;
301| }
302|
303|
304| bool is_macro() const
305| {
306| return m_is_macro;
307| }
308|
309|
310| bool is_next() const
311| {
312| return m_is_next;
313| }
314|
315| private:
316|
317| SourceFile* m_target;
318|
319|
320| bool m_is_macro;
321|
322|
323| bool m_is_next;
324| };
325|
326|
327|
328|
329|
330|
331|
332| class SourceFile : public cleanup
333| {
334| public:
335|
336| typedef std::vector<SourceFile*> vector;
337|
338|
339| SourceFile(const std::string& filename, const std::string& full_filename, bool is_main);
340|
341|
342|
343| const std::string& filename() const
344| {
345| return m_filename;
346| }
347|
348|
349| const std::string& full_filename() const
350| {
351| return m_full_filename;
352| }
353|
354|
355|
356| bool is_main()
357| {
358| return m_is_main;
359| }
360|
361|
362| Declaration::vector& declarations()
363| {
364| return m_declarations;
365| }
366|
367|
368| const Declaration::vector& declarations() const
369| {
370| return m_declarations;
371| }
372|
373|
374| Include::vector& includes()
375| {
376| return m_includes;
377| }
378|
379|
380| const Include::vector& includes() const
381| {
382| return m_includes;
383| }
384|
385| private:
386|
387| std::string m_filename;
388|
389|
390| std::string m_full_filename;
391|
392|
393| bool m_is_main;
394|
395|
396| Declaration::vector m_declarations;
397|
398|
399| Include::vector m_includes;
400| };
401|
402|
403|
404|
405|
406|
407|
408|
409|
410|
411| class Macro : public Declaration
412| {
413| public:
414|
415| typedef std::vector<std::string> Parameters;
416|
417|
418|
419| Macro(SourceFile* file, int line, const ScopedName& name, Parameters* params, const std::string& text);
420|
421|
422| virtual ~Macro();
423|
424|
425|
426| const Parameters* parameters() const
427| {
428| return m_parameters;
429| }
430|
431|
432| const std::string& text() const
433| {
434| return m_text;
435| }
436|
437|
438| virtual void accept(Visitor*);
439|
440| private:
441|
442| Parameters* m_parameters;
443|
444|
445| std::string m_text;
446| };
447|
448|
449|
450|
451|
452| class Scope : public Declaration
453| {
454| public:
455|
456| Scope(SourceFile* file, int line, const std::string& type, const ScopedName& name);
457|
458|
459|
460| virtual ~Scope();
461|
462|
463| virtual void accept(Visitor*);
464|
465|
466|
467|
468|
469|
470| const Declaration::vector& declarations() const
471| {
472| return m_declarations;
473| }
474|
475|
476|
477|
478| Declaration::vector& declarations()
479| {
480| return m_declarations;
481| }
482|
483| private:
484|
485| Declaration::vector m_declarations;
486| };
487|
488|
489|
490| class Namespace : public Scope
491| {
492| public:
493|
494| Namespace(SourceFile* file, int line, const std::string& type, const ScopedName& name);
495|
496| virtual
497| ~Namespace();
498|
499|
500| virtual void
501| accept(Visitor*);
502| }
503| ;
504|
505|
506|
507|
508|
509|
510| class Inheritance
511| {
512| public:
513|
514| typedef std::vector<Inheritance*> vector;
515|
516|
517| typedef std::vector<std::string> Attributes;
518|
519|
520| Inheritance(Types::Type* parent, const Attributes& attributes);
521|
522|
523| void accept(Visitor*);
524|
525|
526|
527|
528|
529|
530|
531|
532|
533| Types::Type* parent()
534| {
535| return m_parent;
536| }
537|
538|
539| const Attributes& attributes() const
540| {
541| return m_attrs;
542| }
543|
544| private:
545|
546| Types::Type* m_parent;
547|
548| Attributes m_attrs;
549| };
550|
551|
552|
553| class Class : public Scope
554| {
555| public:
556|
557| Class(SourceFile* file, int line, const std::string& type, const ScopedName& name);
558|
559|
560| virtual ~Class();
561|
562|
563| virtual void accept(Visitor*);
564|
565|
566|
567|
568|
569|
570| const Inheritance::vector& parents() const
571| {
572| return m_parents;
573| }
574|
575|
576|
577|
578| Inheritance::vector& parents()
579| {
580| return m_parents;
581| }
582|
583|
584| Types::Template* template_type()
585| {
586| return m_template;
587| }
588|
589|
590| void set_template_type(Types::Template* type)
591| {
592| m_template = type;
593| }
594|
595| private:
596|
597| Inheritance::vector m_parents;
598|
599| Types::Template* m_template;
600| };
601|
602|
603|
604| class Forward : public Declaration
605| {
606| public:
607|
608| Forward(SourceFile* file, int line, const std::string& type, const ScopedName& name);
609|
610| Forward(AST::Declaration* decl);
611|
612|
613| virtual void accept(Visitor*);
614|
615|
616| Types::Template* template_type()
617| {
618| return m_template;
619| }
620|
621|
622| void set_template_type(Types::Template* type)
623| {
624| m_template = type;
625| }
626|
627| private:
628|
629| Types::Template* m_template;
630| };
631|
632|
633|
634| class Typedef : public Declaration
635| {
636| public:
637|
638| Typedef(SourceFile* file, int line, const std::string& type, const ScopedName& name, Types::Type* alias, bool constr);
639|
640|
641| ~Typedef();
642|
643|
644| virtual void accept(Visitor*);
645|
646|
647|
648|
649|
650|
651| Types::Type* alias()
652| {
653| return m_alias;
654| }
655|
656|
657| bool constructed()
658| {
659| return m_constr;
660| }
661|
662| private:
663|
664| Types::Type* m_alias;
665|
666|
667| bool m_constr;
668| };
669|
670|
671|
672| class Variable : public Declaration
673| {
674| public:
675|
676| typedef std::vector<size_t> Sizes;
677|
678|
679| Variable(SourceFile* file, int line, const std::string& type, const ScopedName& name, Types::Type* vtype, bool constr);
680|
681|
682| ~Variable();
683|
684|
685| virtual void accept(Visitor*);
686|
687|
688|
689|
690|
691|
692| Types::Type* vtype() const
693| {
694| return m_vtype;
695| }
696|
697|
698| bool constructed() const
699| {
700| return m_constr;
701| }
702|
703|
704| Sizes& sizes()
705| {
706| return m_sizes;
707| }
708|
709| private:
710|
711| Types::Type* m_vtype;
712|
713|
714| bool m_constr;
715|
716|
717| Sizes m_sizes;
718| };
719|
720|
721|
722|
723|
724| class Enumerator : public Declaration
725| {
726| public:
727|
728| typedef std::vector<Enumerator*> vector;
729|
730|
731| Enumerator(SourceFile* file, int line, const std::string& type, const ScopedName& name, const std::string& value);
732|
733|
734| virtual void accept(Visitor*);
735|
736|
737|
738|
739|
740|
741| const std::string& value() const
742| {
743| return m_value;
744| }
745|
746| private:
747|
748| std::string m_value;
749| };
750|
751|
752|
753| class Enum : public Declaration
754| {
755| public:
756|
757| Enum(SourceFile* file, int line, const std::string& type, const ScopedName& name);
758|
759| ~Enum();
760|
761|
762| virtual void
763| accept(Visitor*);
764|
765|
766|
767|
768|
769|
770| Enumerator::vector& enumerators()
771| {
772| return m_enums;
773| }
774|
775| private:
776|
777| Enumerator::vector m_enums;
778| };
779|
780|
781|
782| class Const : public Declaration
783| {
784| public:
785|
786| Const(SourceFile* file, int line, const std::string& type, const ScopedName& name, Types::Type* ctype, const std::string& value);
787|
788|
789| virtual void accept(Visitor*);
790|
791|
792|
793|
794|
795|
796| Types::Type* ctype()
797| {
798| return m_ctype;
799| }
800|
801|
802| const std::string& value() const
803| {
804| return m_value;
805| }
806|
807| private:
808|
809| Types::Type* m_ctype;
810|
811|
812| std::string m_value;
813| };
814|
815|
816|
817| class Parameter : public cleanup
818| {
819| public:
820|
821| typedef std::vector<std::string> Mods;
822|
823|
824| typedef std::vector<Parameter*> vector;
825|
826|
827| Parameter(const Mods& pre, Types::Type* type, const Mods& post, const std::string& name, const std::string& value);
828|
829|
830| ~Parameter();
831|
832|
833|
834| void accept(Visitor*);
835|
836|
837|
838|
839|
840|
841| Mods& premodifier()
842| {
843| return m_pre;
844| }
845|
846|
847| Mods& postmodifier()
848| {
849| return m_post;
850| }
851|
852|
853| Types::Type* type()
854| {
855| return m_type;
856| }
857|
858|
859| const Types::Type* type() const
860| {
861| return m_type;
862| }
863|
864|
865| const std::string& name() const
866| {
867| return m_name;
868| }
869|
870|
871| const std::string& value() const
872| {
873| return m_value;
874| }
875|
876|
877| void set_name(const std::string& name)
878| {
879| m_name = name;
880| }
881| private:
882| Mods m_pre, m_post;
883| Types::Type* m_type;
884| std::string m_name, m_value;
885| };
886|
887|
888|
889|
890|
891|
892| class Function : public Declaration
893| {
894| public:
895|
896| typedef std::vector<std::string> Mods;
897|
898|
899| typedef std::vector<Function*> vector;
900|
901|
902| Function(
903| SourceFile* file, int line, const std::string& type, const ScopedName& name,
904| const Mods& premod, Types::Type* ret, const std::string& realname
905| );
906|
907|
908| ~Function();
909|
910|
911| virtual void accept(Visitor*);
912|
913|
914|
915|
916|
917|
918| Mods& premodifier()
919| {
920| return m_pre;
921| }
922|
923|
924| Types::Type* return_type()
925| {
926| return m_ret;
927| }
928|
929|
930| const std::string& realname() const
931| {
932| return m_realname;
933| }
934|
935|
936| Parameter::vector& parameters()
937| {
938| return m_params;
939| }
940|
941|
942| Types::Template* template_type()
943| {
944| return m_template;
945| }
946|
947|
948| void set_template_type(Types::Template* type)
949| {
950| m_template = type;
951| }
952| private:
953|
954| Mods m_pre;
955|
956| Types::Type* m_ret;
957|
958| std::string m_realname;
959|
960| Parameter::vector m_params;
961|
962| Types::Template* m_template;
963| };
964|
965|
966|
967| class Operation : public Function
968| {
969| public:
970|
971| Operation(SourceFile* file, int line, const std::string& type, const ScopedName& name, const Mods& modifiers, Types::Type* ret, const std::string& realname);
972|
973|
974| virtual void
975| accept(Visitor*);
976| };
977|
978|
979|
980|
981|
982|
983|
984| class Visitor
985| {
986| public:
987|
988| virtual ~Visitor() = 0;
989| virtual void visit_declaration(Declaration*);
990| virtual void visit_macro(Macro*);
991| virtual void visit_scope(Scope*);
992| virtual void visit_namespace(Namespace*);
993| virtual void visit_class(Class*);
994| virtual void visit_inheritance(Inheritance*);
995| virtual void visit_forward(Forward*);
996| virtual void visit_typedef(Typedef*);
997| virtual void visit_variable(Variable*);
998| virtual void visit_const(Const*);
999| virtual void visit_enum(Enum*);
1000| virtual void visit_enumerator(Enumerator*);
1001| virtual void visit_function(Function*);
1002| virtual void visit_operation(Operation*);
1003| virtual void visit_parameter(Parameter*);
1004| };
1005|
1006| }
1007|
1008| #endif // header guard
1009|
1010|