| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 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 | // CompoundLiteral.cpp -- Use variables to implement compound literals.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Mon Nov 15 16:33:00 2021
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Mon Nov 16  9:47:00 2021
 | 
|---|
| 13 | // Update Count     : 0
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "CompoundLiteral.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Decl.hpp"
 | 
|---|
| 19 | #include "AST/Expr.hpp"
 | 
|---|
| 20 | #include "AST/Pass.hpp"
 | 
|---|
| 21 | #include "AST/TranslationUnit.hpp"
 | 
|---|
| 22 | #include "Common/UniqueName.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | namespace Validate {
 | 
|---|
| 25 | 
 | 
|---|
| 26 | namespace {
 | 
|---|
| 27 | 
 | 
|---|
| 28 | struct CompoundLiteral final :
 | 
|---|
| 29 |                 public ast::WithDeclsToAdd<> {
 | 
|---|
| 30 |         ast::Storage::Classes storageClasses;
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         void previsit( const ast::ObjectDecl * decl );
 | 
|---|
| 33 |         const ast::Expr * postvisit( const ast::CompoundLiteralExpr * expr );
 | 
|---|
| 34 | };
 | 
|---|
| 35 | 
 | 
|---|
| 36 | void CompoundLiteral::previsit( const ast::ObjectDecl * decl ) {
 | 
|---|
| 37 |         storageClasses = decl->storage;
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | const ast::Expr * CompoundLiteral::postvisit(
 | 
|---|
| 41 |                 const ast::CompoundLiteralExpr * expr ) {
 | 
|---|
| 42 |         static UniqueName litName( "_compLit" );
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         // Transform: [storageClasses] ... (struct S){...} ...
 | 
|---|
| 45 |         // Into:      [storageClasses] struct S _compLit = {...};
 | 
|---|
| 46 |         //                             ... _compLit ...
 | 
|---|
| 47 |         ast::ObjectDecl * temp = new ast::ObjectDecl(
 | 
|---|
| 48 |                 expr->location,
 | 
|---|
| 49 |                 litName.newName(),
 | 
|---|
| 50 |                 expr->result,
 | 
|---|
| 51 |                 expr->init,
 | 
|---|
| 52 |                 storageClasses
 | 
|---|
| 53 |                 );
 | 
|---|
| 54 |         declsToAddBefore.push_back( temp );
 | 
|---|
| 55 |         return new ast::VariableExpr( expr->location, temp );
 | 
|---|
| 56 | }
 | 
|---|
| 57 | 
 | 
|---|
| 58 | } // namespace
 | 
|---|
| 59 | 
 | 
|---|
| 60 | void handleCompoundLiterals( ast::TranslationUnit & translationUnit ) {
 | 
|---|
| 61 |         ast::Pass<CompoundLiteral>::run( translationUnit );
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | } // namespace Validate
 | 
|---|
| 65 | 
 | 
|---|
| 66 | // Local Variables: //
 | 
|---|
| 67 | // tab-width: 4 //
 | 
|---|
| 68 | // mode: c++ //
 | 
|---|
| 69 | // compile-command: "make install" //
 | 
|---|
| 70 | // End: //
 | 
|---|