source: src/AST/porting.md @ 1e97287

arm-ehcleanup-dtorsjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 1e97287 was 1e97287, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Re-created the statement section of the AST.

  • Property mode set to 100644
File size: 6.6 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 `const Node * accept( Visitor & v ) const = 0` with, e.g. `const Stmt * accept( Visitor & v ) const 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  * because friendship is not inherited, all implementations of clone need
30      /// Must be copied in ALL derived classes
31      template<typename node_t>
32      friend auto mutate(const node_t * node);
33
34All leaves of the `Node` inheritance tree are now declared `final`
35* e.g. `class CompoundStmt final : public Stmt`
36* allows compiler to optimize virtual calls to static calls if given static type
37
38Pulled `FuncSpecifiers` and `StorageClasses` out of `Type` into their own headers
39* Made `BFCommon` a `MakeBitfield` macro in its own header
40  * added default and field-init constructors to macro
41
42Prefer move semantics for containers passed to node constructors
43
44## Code Style ##
45
46### Files ###
47* Headers have a `.hpp` suffix
48* Source code has a `.cpp` suffix
49* All source has the project-standard leading and trailing comments
50* prefer `#pragma once` over `#ifdef` guards
51* namespaces that cover entire files don't get indented
52
53### Documentation ###
54* class, method, and field comments should use doxygen-style `///` prefix
55  * should be included on all classes
56  * 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
57* use explanatory comments with `//` wherever appropriate
58  * older comments should be maintained in porting process wherever possible
59
60### Naming ###
61* Preserve names from previous AST whenever reasonable, and get team consensus on any changes.
62* Strong justification required for private fields
63  * No `get_` prefix on getters (including for generated fields)
64    * exception is `DeclWithType::get_type()`
65* Notable changes:
66  * for concision and consistency with subclasses:
67    * `Declaration` => `ast::Decl`
68        * `DeclarationWithType` => `ast::DeclWithType`
69        * `Expression` => `ast::Expr`
70        * `Initializer` => `ast::Init`
71    * `Statement` => `ast::Stmt`
72  * because they don't really belong to `Type` (and for consistency with `Linkage::Spec`):
73    * `Type::StorageClasses` => `ast::Storage::Classes`
74          * `Type::Extern` etc. => `ast::Storage::Extern` etc.
75        * `LinkageSpec::Spec` => `ast::Linkage::Spec`
76          * `LinkageSpec::Mangle` etc. => `ast::Linkage::Mangle` etc.
77          * `LinkageSpec::linkageUpdate` => `ast::Linkage::update`
78          * `LinkageSpec::linkageName` => `ast::Linkage::name`
79          * `LinkageSpec::isMangled(Spec)` etc. => `Spec.is_mangled` etc.
80          * `LinkageSpec::Intrinsic` etc. => `ast::Linkage::Intrinsic` etc.
81
82## Specific Nodes ##
83`Decl`
84* `storageClasses` => `storage`
85* `declFromId()` => `fromId()`
86  * not 100% sure about the return type here...
87
88`DeclWithType`
89* When `SymTab::Validate::Pass2` is rewritten, update comment on `mangleName` with new name of pass
90* `get_scopedMangleName()` => `scopedMangleName()`
91* `get_type()` now returns `const Type*` so can't be inadvertently mutated
92  * still with `get_` name so it doesn't conflict with subclass field names
93
94`ObjectDecl`
95* changed constructor parameter order for better defaults
96  * allows `newObject` as just default settings
97
98`TypeDecl`
99* stripped `isComplete()` accessor in favour of direct access to `sized`
100
101`EnumDecl`
102* **TODO** rebuild `eval` for new AST (re: `valueOf` implementation)
103
104`Expr`
105* Merged `inferParams`/`resnSlots` into union, as suggested by comment in old version
106  * does imply get_/set_ API, and some care about moving backward
107
108`Label`
109* `get_statement()` exclusively used for code location, replaced with `CodeLocation` field
110
111`CaseStmt`
112* `_isDefault` has been removed
113  * `isDefault` calculates value from `cond`
114  * default may not have a condition. I believe case (!default) requires a condition.
115
116`BranchStmt`
117* `Type` -> `Kind` and `type` -> `kind`
118* Constructors no longer throw SemanticErrorException:
119  * `label` constructor claims it is now considered a syntax error, replaced with assert.
120  * `computedTarget` constructor assumes `Goto`, other check would have SegFaulted.
121
122`TryStmt`
123* `block` -> `body` and `finallyBlock` -> `finally`
124
125`FinallyStmt`
126* `block` -> `body`
127
128`CompoundStmt`
129* **TODO** port copy operator
130  * Needs to be an almost-shallow clone, where the declarations are cloned only if needed
131  * **TODO** port DeclReplacer
132* Still a `std::list` for children, rather than `std::vector`
133  * allows more-efficient splicing for purposes of later code generation
134
135`Type`
136* `forall` field split off into `ParameterizedType` subclass
137  * any type that needs it can inherit from `ParameterizedType`
138* `get_qualifiers()` replaced with accessor `qualifiers()` and mutator `set_qualifiers()`
139  * `get_CV()` replaced with `is_CV()` variants
140* A number of features only supported on aggregates pushed down to `ReferenceToType`:
141  * `attributes`: per docs [1] GCC only supports type attributes on aggregates and typedefs
142    * suggest adding a `TypeWithAttributes` wrapper type if this proves insufficient
143  * `getAggr()`
144  * `genericSubstitution()`
145
146`BasicType`
147* does not inherit from `Type` to allow pointer inlining
148* moved `Kind` before qualifiers in constructor to allow for default
149* **TODO** move `kind`, `typeNames` into code generator
150
151[1] https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Type-Attributes.html#Type-Attributes
152
Note: See TracBrowser for help on using the repository browser.