source: src/AST/porting.md@ 264e691

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 264e691 was fdbd4fd, checked in by Aaron Moss <a3moss@…>, 7 years ago

Add dynamic_cast as method to ptr_base

  • Property mode set to 100644
File size: 5.8 KB
Line 
1# Porting notes for new AST #
2
3## Pointer Types ##
4* raw pointer `T*` is used for construction, but not storage
5* `ast::ptr_base<T,R>` is a pointer to AST node `T` with reference type `R`
6 * specialization: strong pointer `ast::ptr<T>` is used for an ownership relationship
7 * specialization: weak pointer `ast::readonly<T>` is used for an observation relationship
8 * added `ast::ptr_base<T,R>::as<S>()` with same semantics as `dynamic_cast<S*>(p)`
9
10## Visitors ##
11* `Visitor` and `Mutator` are combined into a single `ast::Visitor` class
12 * Base nodes now override `Node* accept( Visitor& v ) = 0` with, e.g. `Stmt* accept( Visitor& v ) override = 0`
13* `PassVisitor` is replaced with `ast::Pass`
14
15## Structural Changes ##
16`CodeLocation` has been devolved from `BaseSyntaxNode` to `ast::ParseNode`
17* excludes `ast::Type` from carrying location information
18* `CodeLocation` is a mandatory constructor field for `ast::ParseNode`
19 * all subclass constructors must fill it; by convention, from their first argument
20
21`N->print(std::ostream&)` is a visitor now, port these methods to `ast::Print` class
22* **TODO** write this visitor
23* **TODO** write `std::ostream& operator<< ( std::ostream& out, const Node* node )` in `Node.hpp` in terms of `ast::Print`
24* `Declaration::printShort` should also be integrated
25
26`clone` is private to `Node` now
27* still needs to be overriden to return appropriate type
28 * e.g. `private: virtual Stmt* clone() const override = 0;`
29
30All leaves of the `Node` inheritance tree are now declared `final`
31* e.g. `class CompoundStmt final : public Stmt`
32* allows compiler to optimize virtual calls to static calls if given static type
33
34Pulled `FuncSpecifiers` and `StorageClasses` out of `Type` into their own headers
35* Made `BFCommon` a `MakeBitfield` macro in its own header
36 * added default and field-init constructors to macro
37
38Prefer move semantics for containers passed to node constructors
39
40## Code Style ##
41
42### Files ###
43* Headers have a `.hpp` suffix
44* Source code has a `.cpp` suffix
45* All source has the project-standard leading and trailing comments
46* prefer `#pragma once` over `#ifdef` guards
47* namespaces that cover entire files don't get indented
48
49### Documentation ###
50* class, method, and field comments should use doxygen-style `///` prefix
51 * should be included on all classes
52 * should be included on any method declaration that doesn't have an obvious behaviour from either naming convention (e.g. constructor, print operator, implement visitor pattern) or an inline implementation
53* use explanatory comments with `//` wherever appropriate
54 * older comments should be maintained in porting process wherever possible
55
56### Naming ###
57* Preserve names from previous AST whenever reasonable, and get team consensus on any changes.
58* Strong justification required for private fields
59 * No `get_` prefix on getters (including for generated fields)
60 * exception is `DeclWithType::get_type()`
61* Notable changes:
62 * for concision and consistency with subclasses:
63 * `Declaration` => `ast::Decl`
64 * `DeclarationWithType` => `ast::DeclWithType`
65 * `Expression` => `ast::Expr`
66 * `Initializer` => `ast::Init`
67 * `Statement` => `ast::Stmt`
68 * because they don't really belong to `Type` (and for consistency with `Linkage::Spec`):
69 * `Type::StorageClasses` => `ast::Storage::Classes`
70 * `Type::Extern` etc. => `ast::Storage::Extern` etc.
71 * `LinkageSpec::Spec` => `ast::Linkage::Spec`
72 * `LinkageSpec::Mangle` etc. => `ast::Linkage::Mangle` etc.
73 * `LinkageSpec::linkageUpdate` => `ast::Linkage::update`
74 * `LinkageSpec::linkageName` => `ast::Linkage::name`
75 * `LinkageSpec::isMangled(Spec)` etc. => `Spec.is_mangled` etc.
76 * `LinkageSpec::Intrinsic` etc. => `ast::Linkage::Intrinsic` etc.
77
78## Specific Nodes ##
79`Decl`
80* `storageClasses` => `storage`
81* `declFromId()` => `fromId()`
82 * not 100% sure about the return type here...
83
84`DeclWithType`
85* When `SymTab::Validate::Pass2` is rewritten, update comment on `mangleName` with new name of pass
86* `get_scopedMangleName()` => `scopedMangleName()`
87* `get_type()` now returns `const Type*` so can't be inadvertently mutated
88 * still with `get_` name so it doesn't conflict with subclass field names
89
90`ObjectDecl`
91* changed constructor parameter order for better defaults
92 * allows `newObject` as just default settings
93
94`TypeDecl`
95* stripped `isComplete()` accessor in favour of direct access to `sized`
96
97`EnumDecl`
98* **TODO** rebuild `eval` for new AST (re: `valueOf` implementation)
99
100`Expr`
101* Merged `inferParams`/`resnSlots` into union, as suggested by comment in old version
102 * does imply get_/set_ API, and some care about moving backward
103
104`Label`
105* `get_statement()` exclusively used for code location, replaced with `CodeLocation` field
106
107`CompoundStmt`
108* **TODO** port copy operator
109 * Needs to be an almost-shallow clone, where the declarations are cloned only if needed
110 * **TODO** port DeclReplacer
111* Still a `std::list` for children, rather than `std::vector`
112 * allows more-efficient splicing for purposes of later code generation
113
114`Type`
115* `forall` field split off into `ParameterizedType` subclass
116 * any type that needs it can inherit from `ParameterizedType`
117* `get_qualifiers()` replaced with accessor `qualifiers()` and mutator `set_qualifiers()`
118 * `get_CV()` replaced with `is_CV()` variants
119* A number of features only supported on aggregates pushed down to `ReferenceToType`:
120 * `attributes`: per docs [1] GCC only supports type attributes on aggregates and typedefs
121 * suggest adding a `TypeWithAttributes` wrapper type if this proves insufficient
122 * `getAggr()`
123 * `genericSubstitution()`
124
125`BasicType`
126* does not inherit from `Type` to allow pointer inlining
127* moved `Kind` before qualifiers in constructor to allow for default
128* **TODO** move `kind`, `typeNames` into code generator
129
130[1] https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Type-Attributes.html#Type-Attributes
131
Note: See TracBrowser for help on using the repository browser.