| 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 |
|
|---|
| 34 | All 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 |
|
|---|
| 38 | Pulled `FuncSpecifiers`, `StorageClasses`, `CVQualifiers` 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 |
|
|---|
| 42 | Prefer 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 | * any field names should follow a similar renaming
|
|---|
| 73 | * because they don't really belong to `Type` (and for consistency with `Linkage::Spec`):
|
|---|
| 74 | * `Type::StorageClasses` => `ast::Storage::Classes`
|
|---|
| 75 | * `Type::Extern` etc. => `ast::Storage::Extern` etc.
|
|---|
| 76 | * `Type::FuncSpecifiers` => `ast::Function::Specs`
|
|---|
| 77 | * `Type::Inline` etc. => `ast::Function::Inline` etc.
|
|---|
| 78 | * `Type::Qualifiers` => `ast::CV::Qualifiers`
|
|---|
| 79 | * `Type::Const` etc. => `ast::CV::Const`
|
|---|
| 80 | * couldn't break name-dependency loop without pulling `Qualifiers` out of `Type`
|
|---|
| 81 | * `LinkageSpec::Spec` => `ast::Linkage::Spec`
|
|---|
| 82 | * `LinkageSpec::Mangle` etc. => `ast::Linkage::Mangle` etc.
|
|---|
| 83 | * `LinkageSpec::linkageUpdate` => `ast::Linkage::update`
|
|---|
| 84 | * `LinkageSpec::linkageName` => `ast::Linkage::name`
|
|---|
| 85 | * `LinkageSpec::isMangled(Spec)` etc. => `Spec.is_mangled` etc.
|
|---|
| 86 | * `LinkageSpec::Intrinsic` etc. => `ast::Linkage::Intrinsic` etc.
|
|---|
| 87 | * Boolean constructor parameters get replaced with a dedicated flag enum:
|
|---|
| 88 | * e.g. `bool isVarLen;` => `enum LengthFlag { FixedLen, VariableLen };` `LengthFlag isVarLen;`
|
|---|
| 89 | * field can be *read* in the existing boolean contexts, but requires documentation to write
|
|---|
| 90 | * suggest naming all flag enums `FooFlag` to hint at boolean nature
|
|---|
| 91 |
|
|---|
| 92 | ## Specific Nodes ##
|
|---|
| 93 | `Decl`
|
|---|
| 94 | * `storageClasses` => `storage`
|
|---|
| 95 | * `declFromId()` => `fromId()`
|
|---|
| 96 | * not 100% sure about the return type here...
|
|---|
| 97 |
|
|---|
| 98 | `DeclWithType`
|
|---|
| 99 | * When `SymTab::Validate::Pass2` is rewritten, update comment on `mangleName` with new name of pass
|
|---|
| 100 | * `get_scopedMangleName()` => `scopedMangleName()`
|
|---|
| 101 | * `get_type()` now returns `const Type*` so can't be inadvertently mutated
|
|---|
| 102 | * still with `get_` name so it doesn't conflict with subclass field names
|
|---|
| 103 |
|
|---|
| 104 | `ObjectDecl`
|
|---|
| 105 | * changed constructor parameter order for better defaults
|
|---|
| 106 | * allows `newObject` as just default settings
|
|---|
| 107 |
|
|---|
| 108 | `TypeDecl`
|
|---|
| 109 | * moved `TypeDecl::Kind` to `ast::TypeVar::Kind`
|
|---|
| 110 |
|
|---|
| 111 | `EnumDecl`
|
|---|
| 112 | * **TODO** rebuild `eval` for new AST (re: `valueOf` implementation)
|
|---|
| 113 |
|
|---|
| 114 | `Expr`
|
|---|
| 115 | * Merged `inferParams`/`resnSlots` into union, as suggested by comment in old version
|
|---|
| 116 | * does imply get_/set_ API, and some care about moving backward
|
|---|
| 117 |
|
|---|
| 118 | `Init`
|
|---|
| 119 | * `bool maybeConstruct` => `enum ConstructFlag { DoConstruct, MaybeConstruct }`
|
|---|
| 120 |
|
|---|
| 121 | `Label`
|
|---|
| 122 | * `get_statement()` exclusively used for code location, replaced with `CodeLocation` field
|
|---|
| 123 |
|
|---|
| 124 | `CaseStmt`
|
|---|
| 125 | * `_isDefault` has been removed
|
|---|
| 126 | * `isDefault` calculates value from `cond`
|
|---|
| 127 | * default may not have a condition. I believe case (!default) requires a condition.
|
|---|
| 128 |
|
|---|
| 129 | `BranchStmt`
|
|---|
| 130 | * `Type` -> `Kind` and `type` -> `kind`
|
|---|
| 131 | * Constructors no longer throw SemanticErrorException:
|
|---|
| 132 | * `label` constructor claims it is now considered a syntax error, replaced with assert.
|
|---|
| 133 | * `computedTarget` constructor assumes `Goto`, other check would have SegFaulted.
|
|---|
| 134 |
|
|---|
| 135 | `TryStmt`
|
|---|
| 136 | * `block` -> `body` and `finallyBlock` -> `finally`
|
|---|
| 137 |
|
|---|
| 138 | `FinallyStmt`
|
|---|
| 139 | * `block` -> `body`
|
|---|
| 140 |
|
|---|
| 141 | `CompoundStmt`
|
|---|
| 142 | * **TODO** port copy operator
|
|---|
| 143 | * Needs to be an almost-shallow clone, where the declarations are cloned only if needed
|
|---|
| 144 | * **TODO** port `DeclReplacer`
|
|---|
| 145 | * Still a `std::list` for children, rather than `std::vector`
|
|---|
| 146 | * allows more-efficient splicing for purposes of later code generation
|
|---|
| 147 |
|
|---|
| 148 | `Type`
|
|---|
| 149 | * `CV::Qualifiers` moved to end of constructor parameter list, defaulted to `{}`
|
|---|
| 150 | * `ReferenceToType` puts a defaulted list of attributes after qualifiers
|
|---|
| 151 | * `forall` field split off into `ParameterizedType` subclass
|
|---|
| 152 | * any type that needs it can inherit from `ParameterizedType`
|
|---|
| 153 | * currently `FunctionType`, `ReferenceToType`
|
|---|
| 154 | * `get_qualifiers()` replaced with accessor `qualifiers()` and mutator `set_qualifiers()`
|
|---|
| 155 | * `get_const()` etc. replaced with `is_const()` etc. variants
|
|---|
| 156 | * `referenceDepth()` now returns `unsigned` rather than `int`
|
|---|
| 157 | * A number of features only supported on aggregates pushed down to `ReferenceToType`:
|
|---|
| 158 | * `attributes`: per docs [1] GCC only supports type attributes on aggregates and typedefs
|
|---|
| 159 | * suggest adding a `TypeWithAttributes` wrapper type if this proves insufficient
|
|---|
| 160 | * `getAggr()` => `aggr()`
|
|---|
| 161 | * also now returns `const AggregateDecl *`
|
|---|
| 162 | * `genericSubstitution()` moved to own visitor **TODO** write
|
|---|
| 163 |
|
|---|
| 164 | `BasicType`
|
|---|
| 165 | * **TODO** move `kind`, `typeNames` into code generator
|
|---|
| 166 |
|
|---|
| 167 | `ReferenceToType`
|
|---|
| 168 | * deleted `get_baseParameters()` from children
|
|---|
| 169 | * replace with `aggr() ? aggr()->parameters : nullptr`
|
|---|
| 170 | * hoisted `lookup` implementation into parent, made non-virtual
|
|---|
| 171 | * also changed to return vector rather than filling; change back if any great win for reuse
|
|---|
| 172 | * `baseStruct` etc. renamed to `base`
|
|---|
| 173 |
|
|---|
| 174 | `PointerType`/`ArrayType`
|
|---|
| 175 | * `is_array()` => `isArray()`
|
|---|
| 176 | * `bool isVarLen;` => `enum LengthFlag { FixedLen, VariableLen }; LengthFlag isVarLen;`
|
|---|
| 177 | * `bool isStatic;` => `enum DimensionFlag { DynamicDim, StaticDim }; DimensionFlag isStatic;`
|
|---|
| 178 |
|
|---|
| 179 | `FunctionType`
|
|---|
| 180 | * `bool isVarArgs;` => `enum ArgumentFlag { FixedArgs, VariableArgs }; ArgumentFlag isVarArgs;`
|
|---|
| 181 |
|
|---|
| 182 | `TypeInstType`
|
|---|
| 183 | * `bool isFtype` => `TypeVar::Kind kind`
|
|---|
| 184 |
|
|---|
| 185 | `TypeofType`
|
|---|
| 186 | * `bool is_basetypeof` => `enum Kind { Typeof, Basetypeof } kind;`
|
|---|
| 187 |
|
|---|
| 188 | `TupleType`
|
|---|
| 189 | * removed `value_type` typedef due to likely error
|
|---|
| 190 | * if readded, should be `const Type *`
|
|---|
| 191 |
|
|---|
| 192 | `AttrType`
|
|---|
| 193 | * did not port due to deprecation of feature
|
|---|
| 194 | * feature is `type@thing` e.g. `int@MAX`
|
|---|
| 195 |
|
|---|
| 196 | [1] https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Type-Attributes.html#Type-Attributes
|
|---|
| 197 |
|
|---|