source: src/AST/Stmt.cpp @ bd1d279

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since bd1d279 was 6180274, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

more cleanup, make more function parameters const, remove more std::

  • Property mode set to 100644
File size: 2.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Stmt.cpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Wed May  8 13:00:00 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Feb  2 19:01:20 2022
13// Update Count     : 3
14//
15
16#include "Stmt.hpp"
17
18
19#include "DeclReplacer.hpp"
20#include "Type.hpp"
21
22namespace ast {
23
24// --- CompoundStmt
25CompoundStmt::CompoundStmt( const CompoundStmt& other ) : Stmt(other), kids(other.kids) {
26        // when cloning a compound statement, we may end up cloning declarations which
27        // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
28        // does a shallow copy, so the VariableExpr will end up pointing to the original
29        // declaration. If the original declaration is deleted, e.g. because the original
30        // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
31        // find all DeclarationWithType nodes (since a VariableExpr must point to a
32        // DeclarationWithType) in the original CompoundStmt and map them to the cloned
33        // node in the new CompoundStmt ('this'), then replace the Declarations referred to
34        // by each VariableExpr according to the constructed map. Note that only the declarations
35        // in the current level are collected into the map, because child CompoundStmts will
36        // recursively execute this routine. There may be more efficient ways of doing
37        // this.
38        DeclReplacer::DeclMap declMap;
39        auto origit = other.kids.begin();
40        for ( const Stmt * s : kids ) {
41                assert( origit != other.kids.end() );
42                const Stmt * origStmt = *origit++;
43                if ( const DeclStmt * declStmt = dynamic_cast< const DeclStmt * >( s ) ) {
44                        const DeclStmt * origDeclStmt = strict_dynamic_cast< const DeclStmt * >( origStmt );
45                        if ( const DeclWithType * dwt = dynamic_cast< const DeclWithType * > ( declStmt->decl.get() ) ) {
46                                const DeclWithType * origdwt = strict_dynamic_cast< const DeclWithType * > ( origDeclStmt->decl.get() );
47                                assert( dwt->name == origdwt->name );
48                                declMap[ origdwt ] = dwt;
49                        } else assert( ! dynamic_cast< const DeclWithType * > ( origDeclStmt->decl.get() ) );
50                } else assert( ! dynamic_cast< const DeclStmt * > ( s ) );
51        }
52        if ( ! declMap.empty() ) {
53                DeclReplacer::replace( this, declMap );
54        }
55}
56
57// --- BranchStmt
58BranchStmt::BranchStmt( const CodeLocation& loc, Kind kind, Label target, const std::vector<Label>&& labels )
59                : Stmt(loc, std::move(labels)), originalTarget(target), target(target), kind(kind) {
60        // Make sure a syntax error hasn't slipped through.
61        assert( Goto != kind || !target.empty() );
62}
63
64const char * BranchStmt::kindNames[] = {
65    "Goto", "Break", "Continue", "FallThrough", "FallThroughDefault"
66};
67
68}
69
70// Local Variables: //
71// tab-width: 4 //
72// mode: c++ //
73// compile-command: "make install" //
74// End: //
Note: See TracBrowser for help on using the repository browser.