[1c01c58] | 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 | // |
---|
[c92bdcc] | 7 | // Examine.cpp -- Helpers for examining AST code. |
---|
[1c01c58] | 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Wed Sept 2 14:02 2020 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
[4f6dda0] | 12 | // Last Modified On : Fri Dec 10 10:27 2021 |
---|
| 13 | // Update Count : 1 |
---|
[1c01c58] | 14 | // |
---|
| 15 | |
---|
[c92bdcc] | 16 | #include "Common/Examine.hpp" |
---|
[1c01c58] | 17 | |
---|
[4f6dda0] | 18 | #include "AST/Type.hpp" |
---|
[c92bdcc] | 19 | #include "CodeGen/OperatorTable.hpp" |
---|
| 20 | #include "InitTweak/InitTweak.hpp" |
---|
[1c01c58] | 21 | |
---|
[4f6dda0] | 22 | namespace { |
---|
| 23 | |
---|
| 24 | // getTypeofThis but does some extra checks used in this module. |
---|
| 25 | const ast::Type * getTypeofThisSolo( const ast::FunctionDecl * func ) { |
---|
| 26 | if ( 1 != func->params.size() ) { |
---|
| 27 | return nullptr; |
---|
| 28 | } |
---|
| 29 | auto ref = func->type->params.front().as<ast::ReferenceType>(); |
---|
| 30 | return (ref) ? ref->base : nullptr; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | const ast::DeclWithType * isMainFor( |
---|
| 36 | const ast::FunctionDecl * func, ast::AggregateDecl::Aggregate kind ) { |
---|
| 37 | if ( "main" != func->name ) return nullptr; |
---|
| 38 | if ( 1 != func->params.size() ) return nullptr; |
---|
| 39 | |
---|
| 40 | auto param = func->params.front(); |
---|
| 41 | |
---|
| 42 | auto type = dynamic_cast<const ast::ReferenceType *>( param->get_type() ); |
---|
| 43 | if ( !type ) return nullptr; |
---|
| 44 | |
---|
| 45 | auto obj = type->base.as<ast::StructInstType>(); |
---|
| 46 | if ( !obj ) return nullptr; |
---|
| 47 | |
---|
| 48 | if ( kind != obj->base->kind ) return nullptr; |
---|
| 49 | |
---|
| 50 | return param; |
---|
| 51 | } |
---|
| 52 | |
---|
[1c01c58] | 53 | namespace { |
---|
[4f6dda0] | 54 | |
---|
| 55 | const ast::Type * getDestructorParam( const ast::FunctionDecl * func ) { |
---|
| 56 | if ( !CodeGen::isDestructor( func->name ) ) return nullptr; |
---|
| 57 | return getTypeofThisSolo( func ); |
---|
| 58 | } |
---|
| 59 | |
---|
[1c01c58] | 60 | } |
---|
| 61 | |
---|
[4f6dda0] | 62 | bool isDestructorFor( |
---|
| 63 | const ast::FunctionDecl * func, const ast::StructDecl * type_decl ) { |
---|
| 64 | if ( const ast::Type * type = getDestructorParam( func ) ) { |
---|
| 65 | auto stype = dynamic_cast<const ast::StructInstType *>( type ); |
---|
| 66 | return stype && stype->base.get() == type_decl; |
---|
| 67 | } |
---|
| 68 | return false; |
---|
| 69 | } |
---|